在XMLAGG中使用不重复记录 [英] Using distinct in XMLAGG on duplicating records

查看:1301
本文介绍了在XMLAGG中使用不重复记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在sql中使用XMLAGG遇到问题.

I am facing a problem using XMLAGG in sql.

我有一个包含多个可以重复的记录的表.该表包含客户地址和客户名称.

I have a table with multiple records which can be repeated. the Table contains Customer Addresses and Customer Name.

Create Table cust_data(
    cust_name varchar2(30),
    cust_addr_line1 varchar2(300),
    cust_addr_line2 varchar2(300),
    cust_addr_line3 varchar2(300),
    cust_addr_type varchar2(3));

该表可能包含单个客户名称和不同地址类型的多个记录.

The table may contain multiple records for a Single Customer Name and Different Address Types.

此外,单个客户可能也有多个相同类型的地址.

Also, a single customer may have multiple addresses of the same type also.

所以客户可能有类似的地址

so a customer may have addresses like

cust1 address1 curr_address
cust1 address2 old_address
cust1 address3 old_address
cust1 address4 old_address
cust2 address5 curr_address
cust2 address6 old_address

我有一个选择,我想用逗号分隔的格式提取出所有具有old_addresses的客户名称.

I have a select where i want to take out all the customer names with old_addresses in a comma separated format.

使用相同的代码,我使用了以下sql

using the same i used the following sql

 select XMLAGG(XMLELEMENT(E, cust_name || ',')).EXTRACT('//text()')
 from cust_data where cust_addr_type ='old_address';

我得到以下输出:

cust1,cust1,cust1,cust2,

cust1,cust1,cust1,cust2,

如何获得输出为

cust1,cust2

cust1,cust2

请帮助.

编辑#1:

其他表格可以这样:

Create Table cust_info(
    cust_name varchar2(30),
    Cust_account varchar2(300),
    cust_amount_paid varchar2(300),
    cust_amount_pend varchar2(300),
    cust_payment_type varchar2(300));

Create Table payment_master_info(
    pmnt_type varchar2(30),
    pmnt_desc varchar2(300),
    pmnt_rate varchar2(300),
    pmnt_tenure varchar2(300));

查询是这样的:

SELECT XMLAGG(XMLELEMENT(E, CUST_NAME || ',')) .EXTRACT('//text()'),
             CD.CUST_ADDR_LINE1,
             CD.CUST_ADDR_LINE2,
             CD.CUST_ADDR_LINE3,
             CI.CUST_AMOUNT_PAID,
             CI.CUST_AMOUNT_PEND,
             CI.CUST_ACCOUNT
    FROM CUST_INFO CI, PAYMENT_MASTER_INFO PM, CUST_DATA CD
 WHERE CD.CUST_NAME = CI.CUST_NAME
     AND CI.CUST_PAYMENT_TYPE = PM.PMNT_TYPE
     AND CUST_ADDR_TYPE = 'old_address';

现在的数据非常庞大. pmnt_type中的数据范围为10000-15000数据 在其他表格中,范围是2100000-5000000

Now the data in this huge. the data in the pmnt_type ranges from 10000-15000 data and in other tables it ranges from 2100000-5000000

如果我对不同的数据应用内部查询,性能将成指数下降.

If I apply inner queries for distinct data, the performance drops exponentially.

还有其他方法吗?

Edit#2:同样在使用此内部查询时,在执行过程中,我得到一个ORA-19011(:字符串缓冲区太小.)错误.会有什么原因... ??

Edit#2: Also while using this inner query, during executing, i getting an ORA-19011(: Character string buffer too small.) error. can there be any reason why...??

推荐答案

您可以在内部查询中使用DISTINCT:

You can use a DISTINCT in an inner query:

SQL> select XMLAGG(XMLELEMENT(E, cust_name || ',')).EXTRACT('//text()')
  2   from (SELECT distinct cust_name, cust_addr_type FROM cust_data)
  3  where cust_addr_type ='old_address';

XMLAGG(XMLELEMENT
-----------------
cust1,cust2,

这篇关于在XMLAGG中使用不重复记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆