2015-06-25 163 views
0

我使用以下代碼來實現'DEL'文本和'PAL'文本。 'DEL'和'PAL'文本可以跨越幾行(而不一定是每行的相同數量的行)。Oracle sql:使用兩個listagg

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' 

我的問題是,在那裏我有在多行「DEL文本和僅一個上行‘PAL’文本‘PAL’文本重複,例如

enter image description here

的 'PAL_TEXT' 被複制,因爲只有一個PAL_TEXT但存在三個DEL_TEXT存在。

有沒有辦法刪除重複?

謝謝,SMORF

+0

試試這個:HTTP://stackoverflow.com/questions/31003209/oracle -listagg-for-multiple-attributes/31021440#31021440 – ksa

+0

謝謝ksa ...我無法理解的是我在哪裏使用2個不同的表格?這裏的例子只使用1個表格。 – SMORF

+0

我們可以看到實際表格數據的一個例子嗎?每個PAL文本總是有多個DEL文本,或者您是否有兩個具有不同DEL文本的相同PAL文本?我沒有看到這裏的關係。 –

回答

1

不要緊,有多少表中聚集(可惜我不能沒有你的數據結構進行語法檢查):

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) 
+0

非常感謝您的幫助 – SMORF

0

重寫選擇到

1)基團在連接鍵上表和計算LISTAGG(可能除去重複的鍵)

2)連接的結果

連接將始終爲1:1,因此不會導致重複。