リバースwm_concat、LISTAGG

 

1.テストデータ:

with tst as(
select '张三' as name,'1' as type,'Z-5678' as phone from dual
union all
select '张三' as name,'2' as type,'Z-7890' as phone from dual
union all
select '张三' as name,'3' as type,'Z-1001' as phone from dual
union all
select '李四' as name,'1' as type,'L-1237' as phone from dual
union all
select '李四' as name,'3' as type,'L-5600' as phone from dual
union all
select '马五' as name,'1' as type,'M-1378' as phone from dual
union all
select '马五' as name,'2' as type,'M-3948' as phone from dual
union all
select '王二' as name,'2' as type,'W-1245' as phone from dual
)

 

2.重合データ:

CREATE TABLE test1 AS
with tst as(
select '张三' as name,'1' as type,'Z-5678' as phone from dual
union all
select '张三' as name,'2' as type,'Z-7890' as phone from dual
union all
select '张三' as name,'3' as type,'Z-1001' as phone from dual
union all
select '李四' as name,'1' as type,'L-1237' as phone from dual
union all
select '李四' as name,'3' as type,'L-5600' as phone from dual
union all
select '马五' as name,'1' as type,'M-1378' as phone from dual
union all
select '马五' as name,'2' as type,'M-3948' as phone from dual
union all
select '王二' as name,'2' as type,'W-1245' as phone from dual
)SELECT * FROM tst
SELECT t.name,listagg(t.phone,',') WITHIN GROUP(ORDER BY t.phone) phone FROM tst t
GROUP BY t.name;

3.逆方向データ分解:

WITH t AS(
     SELECT t.name,t.phone,1 FROM test1 t
),
A(NAME,phone,lvl) AS(
    SELECT NAME,phone,1 FROM t
    UNION ALL
    SELECT NAME,
    SUBSTR(phone,INSTR(phone,',',1,1)+1),
    lvl+1
     FROM a WHERE INSTR(phone,',',1,1) > 1
)
SELECT name,REGEXP_SUBSTR(phone,'[^,]+',1,1) phone FROM a;

 

優れた効率の道を探しています!

おすすめ

転載: blog.csdn.net/iO_O_Oi/article/details/83186092