안녕하십니까 고수님들~
먼저, 새해 복 많이 받으세요
with ITEM_LIST as (
select '연필' as ITEM from dual
union all
select '지우걔' as ITEM from dual
union all
select '휴지통' as ITEM from dual
union all
select '볼펜' as ITEM from dual
union all
select '메모' as ITEM from dual
union all
select '건전지' as ITEM from dual
union all
select '시계' as ITEM from dual
union all
select '키보드' as ITEM from dual
)
, ROOM_LIST as (
select '20241230' as IPGO_DT
, 'ROOM1' as ROOM_CD
, '연필/지우개/휴지통' as CUS_INFO
from dual
union all
select '20241230' as IPGO_DT
, 'ROOM2' as ROOM_CD
, '볼펜/메모/연필' as CUS_INFO
from dual
union all
select '20241230' as IPGO_DT
, 'ROOM3' as ROOM_CD
, '메모/건전지/볼펜' as CUS_INFO
from dual
union all
select '20241231' as IPGO_DT
, 'ROOM3' as ROOM_CD
, '메모/건전지/볼펜' as CUS_INFO
from dual
union all
select '20241231' as IPGO_DT
, 'ROOM4' as ROOM_CD
, '볼펜' as CUS_INFO
from dual
)
select a.IPGO_DT
-- , listagg(a.ROOM_CD, ',') within group (order by a.ROOM_CD, b.ITEM) as ROOM_DATA
from ROOM_LIST a
, ITEM_LIST b
where instr( '/' || b.ITEM || '/', '/' || a.CUS_INFO || '/') > 0
==> 결과
일자 | 연필 | 지우개 | 휴지통 | 볼펜 | 메모 | 건전지 | 시계 | 키보드 |
20241230 | ROOM1, ROOM2 | ROOM1 | ROOM1 | ROOM2, ROOM3 | ROOM2,ROOM3 | ROOM3 | ||
20241231 | ROOM3,ROOM4 | ROOM3 | ROOM3 |
이렇게 결과가 나오게 부타드립니다
WITH item_list AS ( SELECT '연필' item FROM dual UNION ALL SELECT '지우개' FROM dual UNION ALL SELECT '휴지통' FROM dual UNION ALL SELECT '볼펜' FROM dual UNION ALL SELECT '메모' FROM dual UNION ALL SELECT '건전지' FROM dual UNION ALL SELECT '시계' FROM dual UNION ALL SELECT '키보드' FROM dual ) , room_list AS ( SELECT '20241230' ipgo_dt, 'ROOM1' room_cd, '연필/지우개/휴지통' cus_info FROM dual UNION ALL SELECT '20241230', 'ROOM2', '볼펜/메모/연필' FROM dual UNION ALL SELECT '20241230', 'ROOM3', '메모/건전지/볼펜' FROM dual UNION ALL SELECT '20241231', 'ROOM3', '메모/건전지/볼펜' FROM dual UNION ALL SELECT '20241231', 'ROOM4', '볼펜' FROM dual ) SELECT * FROM (SELECT a.ipgo_dt , b.item , LISTAGG(a.room_cd, ', ') WITHIN GROUP(ORDER BY a.room_cd) room_data FROM room_list a , item_list b WHERE INSTR('/'|| a.cus_info ||'/', '/'|| b.item ||'/') > 0 GROUP BY a.ipgo_dt, b.item ) PIVOT (MIN(room_data) FOR item IN ( '연필' 연필 , '지우개' 지우개 , '휴지통' 휴지통 , '볼펜' 볼펜 , '메모' 메모 , '건전지' 건전지 , '시계' 시계 , '키보드' 키보드 ) ) ORDER BY ipgo_dt ;