逆向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