亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Oracle中的LISTAGG返回不同的值

Oracle中的LISTAGG返回不同的值

桃花長相依 2019-08-27 16:51:39
Oracle中的LISTAGG返回不同的值我試圖LISTAGG在Oracle中使用該功能。我想只獲得該列的不同值。有沒有一種方法可以在不創建函數或過程的情況下獲得不同的值?  col1 col2 Created_by    1 2史密斯     1 2約翰     1 3 Ajay     1 4拉姆     1杰克我需要選擇col1和LISTAGGcol2(不考慮第3列)。當我這樣做時,我得到這樣的結果LISTAGG:[2,2,3,4,5]我需要在這里刪除重復的'2'; 我只需要col2對col1的不同值。
查看完整描述

3 回答

?
慕的地10843

TA貢獻1785條經驗 獲得超8個贊

19c及以后:

select listagg(distinct the_column, ',') within group (order by the_column)from the_table

18c及更早:

select listagg(the_column, ',') within group (order by the_column)from (
   select distinct the_column 
   from the_table) t

如果您需要更多列,那么您可能正在尋找以下內容:

select col1, listagg(col2, ',') within group (order by col2)from (
  select col1, 
         col2,
         row_number() over (partition by col1, col2 order by col1) as rn  from foo  order by col1,col2)where rn = 1group by col1;


查看完整回答
反對 回復 2019-08-27
?
有只小跳蛙

TA貢獻1824條經驗 獲得超8個贊

以下是解決問題的方法。


select  

      regexp_replace(

    '2,2,2.1,3,3,3,3,4,4' 

     ,'([^,]+)(,\1)*(,|$)', '\1\3')


from dual

回報


2,2.1,3,4


答案(見下面的注釋):


select col1, 


regexp_replace(

    listagg(

     col2 , ',') within group (order by col2)  -- sorted

    ,'([^,]+)(,\1)*(,|$)', '\1\3') )

   from tableX

where rn = 1

group by col1; 

注意:以上內容適用于大多數情況 - 列表應該排序,您可能需要根據您的數據修剪所有尾隨和前導空格。


如果你在> 20或大字符串大小的組中有很多項,你可能會遇到oracle字符串大小限制'字符串連接的結果太長'所以在每個組的成員上放一個最大數字。這只有在可以僅列出第一個成員的情況下才有效。如果你有很長的變量字符串,這可能不起作用。你將不得不進行實驗。


select col1,


case 

    when count(col2) < 100 then 

       regexp_replace(

        listagg(col2, ',') within group (order by col2)

        ,'([^,]+)(,\1)*(,|$)', '\1\3')


    else

    'Too many entries to list...'

end


from sometable

where rn = 1

group by col1;

另一種解決方案(沒那么簡單),希望能夠避免oracle的字符串大小限制-字符串大小限制為4000感謝這個職位在這里通過user3465996


select col1  ,

    dbms_xmlgen.convert(  -- HTML decode

    dbms_lob.substr( -- limit size to 4000 chars

    ltrim( -- remove leading commas

    REGEXP_REPLACE(REPLACE(

         REPLACE(

           XMLAGG(

             XMLELEMENT("A",col2 )

               ORDER BY col2).getClobVal(),

             '<A>',','),

             '</A>',''),'([^,]+)(,\1)*(,|$)', '\1\3'),

                  ','), -- remove leading XML commas ltrim

                      4000,1) -- limit to 4000 string size

                      , 1)  -- HTML.decode

                       as col2

 from sometable

where rn = 1

group by col1;

一些測試用例 - 僅供參考


regexp_replace('2,2,2.1,3,3,4,4','([^,]+)(,\1)+', '\1')

-> 2.1,3,4 Fail

regexp_replace('2 ,2 ,2.1,3 ,3 ,4 ,4 ','([^,]+)(,\1)+', '\1')

-> 2 ,2.1,3,4 Success  - fixed length items

項目中包含的項目,例如。2,21


regexp_replace('2.1,1','([^,]+)(,\1)+', '\1')

-> 2.1 Fail

regexp_replace('2 ,2 ,2.1,1 ,3 ,4 ,4 ','(^|,)(.+)(,\2)+', '\1\2')

-> 2 ,2.1,1 ,3 ,4  -- success - NEW regex

 regexp_replace('a,b,b,b,b,c','(^|,)(.+)(,\2)+', '\1\2')

-> a,b,b,c fail!

v3 - 正則表達式感謝伊戈爾!適用于所有情況。


select  

regexp_replace('2,2,2.1,3,3,4,4','([^,]+)(,\1)*(,|$)', '\1\3') ,

---> 2,2.1,3,4 works

regexp_replace('2.1,1','([^,]+)(,\1)*(,|$)', '\1\3'),

--> 2.1,1 works

regexp_replace('a,b,b,b,b,c','([^,]+)(,\1)*(,|$)', '\1\3')

---> a,b,c works


from dual


查看完整回答
反對 回復 2019-08-27
?
哆啦的時光機

TA貢獻1779條經驗 獲得超6個贊

你可以使用未記錄的wm_concat功能。

select col1, wm_concat(distinct col2) col2_list 
from tab1group by col1;

此函數返回clob列,如果您希望可以使用dbms_lob.substr將clob轉換為varchar2。


查看完整回答
反對 回復 2019-08-27
  • 3 回答
  • 0 關注
  • 1056 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號