繁体   English   中英

Listagg function with rep substr function

[英]Listagg function with rep substr function

原始数据:

Col1    Col2    Col3    Col4
Ajay    G1  B1  10.201.131.27
Ajay    G1  B2  10.201.131.27
Ajay    G1  B1  10.201.131.28
Ajay    G1  B2  10.201.131.28
Ajay    G1  B1  10.201.131.29
Ajay    G1  B2  10.201.131.29

预期 OUTPUT 使用 Oracle 10g

Col1    Col2    Col3    Col4
Ajay    G1  B1,B2   10.201.131.27,10.201.131.28, 10.201.131.29

如果有人能够提供帮助将非常高兴。

我使用的查询:

select * from (select 
Col1,
Col2,
substr(regexp_replace(','||(LISTAGG(( CASE WHEN T1.COl3 IS NULL OR TRIM( T1.COl3 ) ='' THEN NULL ELSE T1.COl3   END), ',') WITHIN GROUP (ORDER BY T1.COl1 )), '(,[^,]+)(\1)+', '\1'),2) as COl3 ,
substr(regexp_replace(','||(LISTAGG(( CASE WHEN T1.COl4 IS NULL OR TRIM( T1.COl4 ) ='' THEN NULL ELSE  T1.COl4   END), ',') WITHIN GROUP (ORDER BY T1.COl1 )), '(,[^,]+)(\1)+', '\1'),2) as COl4 ,
from T1
Group by 
Col1
)abc

我得到的输出如下,

Col1    Col2    Col3    Col4
Ajay    G1  B1,B2   10.201.131.27
Ajay    G1  B1,B2   10.201.131.28
Ajay    G1  B1,B2   10.201.131.29

提前致谢。

要从LISTAGG中消除重复,您可以在Oracle 10中使用row_number function 来定义重复的顺序

在下一步中,您仅将第一个重复项(row_number = 1)传递给LISTAGG function,所有更高的重复项都将重置为NULLLISTAGG忽略。

这里是查询

with t2 as (
select 
COL1, COL2,
COL3,
row_number() over (partition by col1, col2, col3 order by null) as rn3,
COL4,
row_number() over (partition by col1, col2, col4 order by null) as rn4
from t)
select
  COL1, COL2,
  listagg(case when rn3 = 1 then COL3 end,',') within group (order by COL3) COL3,
  listagg(case when rn4 = 1 then COL4 end,',') within group (order by COL4) COL4
from t2
group by COL1, COL2

结果

COL1,   COL2, COL3, COL4
Ajay    G1  B1,B2   10.201.131.27,10.201.131.28,10.201.131.29

请注意,对于您经常遇到的非平凡数据,这种方法比使用REGEXP进行消除要ORA-01489: result of string concatenation is too long before you can start the Elimination

另请注意,您可以升级到 Oracle 19(从 Oracle 10 的角度来看可以认为是过期的)并且您可以使用功能LISTAGG (DISTINCT而无需消除重复。此版本还优雅地处理owerflow问题。

with t (Col1,    Col2,    Col3,    Col4) as (
select 'Ajay',    'G1',  'B1',  '10.201.131.27' from dual union all
select 'Ajay',    'G1',  'B2',  '10.201.131.27' from dual union all
select 'Ajay',    'G1',  'B1',  '10.201.131.28' from dual union all
select 'Ajay',    'G1',  'B2',  '10.201.131.28' from dual union all
select 'Ajay',    'G1',  'B1',  '10.201.131.29' from dual union all
select 'Ajay',    'G1',  'B2',  '10.201.131.29' from dual)
, t1 as (
select Col1, Col2
, listagg(Col3, ',') within group (order by Col3) x
, listagg(Col4, ',') within group (order by Col4) y
from t
group by Col1, Col2
)
select Col1, Col2
, rtrim(regexp_replace(x || ',', '([^,]+,)\1+', '\1'), ',') Col3_
, rtrim(regexp_replace(y || ',', '([^,]+,)\1+', '\1'), ',') Col4_
from t1
;

COL1  CO  COL3_                           COL4_                                                       
----  --  ------------------------------  ------------------------------------------------------------
Ajay  G1  B1,B2                           10.201.131.27,10.201.131.28,10.201.131.29                     

我分两步展示它,但它也可以是一步。

select Col1, Col2
, rtrim(regexp_replace(listagg(Col3, ',') within group (order by Col3) || ',', '([^,]+,)\1+', '\1'), ',') Col3_
, rtrim(regexp_replace(listagg(Col4, ',') within group (order by Col4) || ',', '([^,]+,)\1+', '\1'), ',') Col4_
from t
group by Col1, Col2
;

暂无
暂无

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

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