mssql로 행을 열로 보여주려 합니다.
데이터는 아래와 같습니다.
ITEM_DESC는 우선 3개의 타입 고정입니다.
UNPIVOT 후 PIVOT으로 행을 열로 보여주려 하는데 잘 되지 않아서 문의글을 올립니다.
WITH T AS (
SELECT 8 AS SEQ , 'DISP0002' ITEM_DESC , 'C' AS ITEM_TYPE, 'AA' AS OWNER union all
SELECT 8 AS SEQ , 'DISP0003' ITEM_DESC , 'C' AS ITEM_TYPE, 'CC' AS OWNER union all
SELECT 8 AS SEQ , 'DISP0004' ITEM_DESC , 'C' AS ITEM_TYPE, 'BB' AS OWNER union all
SELECT 9 AS SEQ , 'DISP0002' ITEM_DESC , 'I' AS ITEM_TYPE, 'AA' AS OWNER union all
SELECT 9 AS SEQ , 'DISP0003' ITEM_DESC , 'I' AS ITEM_TYPE, 'AA' AS OWNER union all
SELECT 9 AS SEQ , 'DISP0004' ITEM_DESC , 'C' AS ITEM_TYPE, 'DD' AS OWNER
)
SELECT * FROM T
SEQ | ITEM_DESC | ITEM_TYPE | OWNER |
8 | DISP0002 | C | AA |
8 | DISP0003 | C | CC |
8 | DISP0004 | C | BB |
9 | DISP0002 | I | AA |
9 | DISP0003 | I | AA |
9 | DISP0004 | C | DD |
결과
SEQ | DISP0002_TYPE | DISP0002_OWNER | DISP0003_TYPE | DISP0003_OWNER | DISP0004_TYPE | DISP0004_OWNER |
8 | C | AA | C | CC | C | BB |
9 | I | AA | I | AA | C | DD |
WITH T AS ( SELECT 8 seq, 'DISP0002' item_desc, 'C' item_type, 'AA' owner UNION ALL SELECT 8, 'DISP0003', 'C', 'CC' UNION ALL SELECT 8, 'DISP0004', 'C', 'BB' UNION ALL SELECT 9, 'DISP0002', 'I', 'AA' UNION ALL SELECT 9, 'DISP0003', 'I', 'AA' UNION ALL SELECT 9, 'DISP0004', 'C', 'DD' ) SELECT seq , MIN(CASE item_desc WHEN 'DISP0002' THEN item_type END) disp0002_type , MIN(CASE item_desc WHEN 'DISP0002' THEN owner END) disp0002_owner , MIN(CASE item_desc WHEN 'DISP0003' THEN item_type END) disp0003_type , MIN(CASE item_desc WHEN 'DISP0003' THEN owner END) disp0003_owner , MIN(CASE item_desc WHEN 'DISP0004' THEN item_type END) disp0004_type , MIN(CASE item_desc WHEN 'DISP0004' THEN owner END) disp0004_owner FROM t GROUP BY seq ;
Oracle 의 경우 다중 항목으로 PIVOT/UNPIVOT 이 가능합니다.
MSSQL 의 경우 여러가지 시도해 봤는데 안되는 것 같습니다.
WITH t AS ( SELECT 8 seq, 'DISP0002' item_desc, 'C' item_type, 'AA' owner FROM dual UNION ALL SELECT 8, 'DISP0003', 'C', 'CC' FROM dual UNION ALL SELECT 8, 'DISP0004', 'C', 'BB' FROM dual UNION ALL SELECT 9, 'DISP0002', 'I', 'AA' FROM dual UNION ALL SELECT 9, 'DISP0003', 'I', 'AA' FROM dual UNION ALL SELECT 9, 'DISP0004', 'C', 'DD' FROM dual ) -- Oracle -- SELECT * FROM t PIVOT (MIN(item_type) type, MIN(owner) owner FOR item_desc IN ( 'DISP0002' DISP0002 , 'DISP0003' DISP0003 , 'DISP0004' DISP0004 ) ) ;
글쎄요?
지금 언급하신 방식(붙여서 피봇 후 잘라 쓰기)으로 가능할 듯 합니다.
잘 안돼서 질문하시는 건가요?
동적 구현으로 하더라도 잘라쓰는 과정만 더 복잡해 집니다.
MIN(CASE 부분을 동적으로 구현하는 방법이 나을 듯 하네요.
WITH t AS ( SELECT 8 seq, 'DISP0002' item_desc, 'C' item_type, 'AA' owner UNION ALL SELECT 8, 'DISP0003', 'C', 'CC' UNION ALL SELECT 8, 'DISP0004', 'C', 'BB' UNION ALL SELECT 9, 'DISP0002', 'I', 'AA' UNION ALL SELECT 9, 'DISP0003', 'I', 'AA' UNION ALL SELECT 9, 'DISP0004', 'C', 'DD' ) SELECT seq , SUBSTRING(disp0002, 1, 1) disp0002_type , SUBSTRING(disp0002, 2, 9) disp0002_owner , SUBSTRING(disp0003, 1, 1) disp0003_type , SUBSTRING(disp0003, 2, 9) disp0003_owner , SUBSTRING(disp0004, 1, 1) disp0004_type , SUBSTRING(disp0004, 2, 9) disp0004_owner FROM (SELECT seq , item_desc , CONCAT(item_type, owner) v FROM t ) a PIVOT (MIN(v) FOR item_desc IN ([DISP0002], [DISP0003], [DISP0004])) a ;