쿼리 질문 드립니다 0 2 6,773

by 일곱난장이 [SQL Query] [2025.01.02 09:12:20]


안녕하십니까 고수님들~

먼저, 새해 복 많이 받으세요

 

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    

이렇게  결과가 나오게 부타드립니다

 

by 마농 [2025.01.02 09:53:35]
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
;

 


by 일곱난장이 [2025.01.02 10:23:43]

매번 도움 주셔서 너무 감사드립니다

새해 복 많이 받으세요~~~

 

댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입