简体   繁体   English

Oracle sql:使用两个listagg

[英]Oracle sql: Using two listagg

I'm using the following code to bring through 'DEL' text and 'PAL' text. 我正在使用以下代码来显示“ DEL”文本和“ PAL”文本。 The 'DEL' and 'PAL' text could be across several lines (and not necessarily the same amount of lines of each). 'DEL'和'PAL'文本可以跨越多行(并且不一定需要相同的行数)。

select trim(listagg(tx1.text, ', ') within group (order by tx1.text)) del_text,
trim(listagg(tx2.text, ', ') within group (order by tx2.text)) pal_text
from oes_ordtxt tx1
inner join oes_ordtxt tx2
    on tx1.key1 = tx2.key1
    and tx1.key2 = tx2.key2
    and tx1.key3 = tx2.key3
    and tx2.doctyp = 'PAL'
where tx1.key1 = '0018104834'
and tx1.key2 = '00001'
and tx1.key3 = '001'
and tx1.doctyp = 'DEL'

The problem I have is that where I have multiple rows on 'DEL text and only one row on 'PAL' text the 'PAL' text repeats, eg 我的问题是,在“ DEL文本”上有多行而在“ PAL”文本上只有一行的情况下,“ PAL”文本会重复,例如

在此处输入图片说明

The 'PAL_TEXT' is duplicating as only one PAL_TEXT exists but, three DEL_TEXT exists. 由于仅存在一个PAL_TEXT,但是存在三个DEL_TEXT,因此正在复制“ PAL_TEXT”。

Is there a way to remove the duplicates? 有没有办法删除重复项?

Thanks, SMORF 谢谢,SMORF

It doesn't matter how many tables in aggregation (unfortunately I can't check syntax without your data structure): 聚合中有多少张表都没有关系(不幸的是,没有数据结构我无法检查语法):

select (select listagg(column_value,', ') within group (order by column_value) from table (del_text)) del_text
      ,(select listagg(column_value,', ') within group (order by column_value) from table (pal_text)) pal_text
from (select collect (distinct tx1.text) del_text,
             collect (distinct tx2.text) pal_text
      from oes_ordtxt tx1
      inner join oes_ordtxt tx2
         on tx1.key1 = tx2.key1
        and tx1.key2 = tx2.key2
        and tx1.key3 = tx2.key3
        and tx2.doctyp = 'PAL'
      where tx1.key1 = '0018104834'
        and tx1.key2 = '00001'
        and tx1.key3 = '001'
        and tx1.doctyp = 'DEL'
      group by 1)

Rewrite the select to 将选择内容重写为

1) group both tables on the join key and calculate listagg (possible removing duplicate keys) 1)将两个表归为联接键并计算listagg(可能删除重复键)

2) join the result 2)加入结果

The join will be always 1:1, so there will be no duplication caused by it. 连接将始终为1:1,因此不会造成任何重复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM