퇴근5분전

피벗으로 하는 건데...

문제 차제가 조금 데이타가 부족해보였다.

그림에 보이는데로 나오는 걸 일부 수정해서 풀었지만...

동적으로 늘어나는 데이타 컬럼에 대해서 처리를 위해 동적쿼리를 만들어야 되는데...

과연? 얼마나 사이즈가 늘어날지는 실 데이타로 처리 해봐야 하니 .. 모르겠고,  

 

문제 : http://www.sqler.com/828698#1

 

## 첫풀이 : 공백으로 처리 했지만 char(10)으로 바꾸면 enter값을 넣어서 뿌리는 건 비슷하게 나온다.


;with tb
as
(
    SELECT ITEM_CD, SPEC, SO_DT + ' ' + DLVY_DT + ' '+  SOLD_TO_PARTY_NM as HD, SO_QTY
    FROM (
        VALUES
            ( '41-P006-IVM', 'GAP-6 IV', '2016-05-10', '2016-05-13', 'TMEH-J', 2400 ),
            ( '41-P006-IVM', 'GAP-6 IV', '2016-05-12', '2016-05-20', 'TMEH-J', 4800 ),
            ( '41-P006-IVM', 'GAP-6 IV', '2016-05-17', '2016-05-20', 'TMEH-J', 10800 ),
            ( '41-P006-IVM', 'GAP-6 IV', '2016-05-13', '2016-05-20', 'AXIS', 2400 )

    ) AS TB ( ITEM_CD, SPEC, SO_DT, DLVY_DT, SOLD_TO_PARTY_NM, SO_QTY )
)
SELECT *
  from tb
    pivot
    (
         sum( SO_QTY ) for HD in (
            [2016-05-13 2016-05-20 AXIS],
            [2016-05-10 2016-05-13 TMEH-J],
            [2016-05-12 2016-05-20 TMEH-J],
            [2016-05-17 2016-05-20 TMEH-J]
         )
    ) as pt

 

 

 

## 두번째 풀이 : 헤더값을 결과로 만들어 Union 처리 하는 방법.

;with tb
as
(
    SELECT ITEM_CD, SPEC, SO_DT, DLVY_DT, SOLD_TO_PARTY_NM, SO_QTY
    FROM (
        VALUES
            ( '41-P006-IVM', 'GAP-6 IV', '2016-05-10', '2016-05-13', 'TMEH-J', 2400 ),
            ( '41-P006-IVM', 'GAP-6 IV', '2016-05-12', '2016-05-20', 'TMEH-J', 4800 ),
            ( '41-P006-IVM', 'GAP-6 IV', '2016-05-17', '2016-05-20', 'TMEH-J', 10800 ),
            ( '41-P006-IVM', 'GAP-6 IV', '2016-05-13', '2016-05-20', 'AXIS', 2400 )

    ) AS TB ( ITEM_CD, SPEC, SO_DT, DLVY_DT, SOLD_TO_PARTY_NM, SO_QTY )
), src
as
(
    select ITEM_CD, SPEC, SO_DT + ' ' + DLVY_DT +' '+ SOLD_TO_PARTY_NM as HD, SO_QTY
    from tb
), hd1
as
(
    select ITEM_CD, SPEC, SO_DT + ' ' + DLVY_DT +' '+ SOLD_TO_PARTY_NM as HD, SO_DT
    from tb
)
, hd2
as
(
    select ITEM_CD, SPEC, SO_DT + ' ' + DLVY_DT +' '+ SOLD_TO_PARTY_NM as HD, DLVY_DT
    from tb
)
, hd3
as
(
    select ITEM_CD, SPEC, SO_DT + ' ' + DLVY_DT +' '+ SOLD_TO_PARTY_NM as HD, SOLD_TO_PARTY_NM
    from tb
)


 
select *, 1 as odr--ITEM_CD, SPEC, [2016-05-13 2016-05-20 AXIS],[2016-05-10 2016-05-13 TMEH-J],[2016-05-12 2016-05-20 TMEH-J],[2016-05-17 2016-05-20 TMEH-J]
from hd1
pivot
(
     min( SO_DT ) for HD in (
        [2016-05-13 2016-05-20 AXIS],
        [2016-05-10 2016-05-13 TMEH-J],
        [2016-05-12 2016-05-20 TMEH-J],
        [2016-05-17 2016-05-20 TMEH-J]
     )
) as pt
union

select *, 2 as odr--ITEM_CD, SPEC, [2016-05-13 2016-05-20 AXIS],[2016-05-10 2016-05-13 TMEH-J],[2016-05-12 2016-05-20 TMEH-J],[2016-05-17 2016-05-20 TMEH-J]
from hd2
pivot
(
    min( DLVY_DT ) for HD in (
        [2016-05-13 2016-05-20 AXIS],
        [2016-05-10 2016-05-13 TMEH-J],
        [2016-05-12 2016-05-20 TMEH-J],
        [2016-05-17 2016-05-20 TMEH-J]
     )
) as pt
union

select *, 3 as odr--ITEM_CD, SPEC, [2016-05-13 2016-05-20 AXIS],[2016-05-10 2016-05-13 TMEH-J],[2016-05-12 2016-05-20 TMEH-J],[2016-05-17 2016-05-20 TMEH-J]
from hd3
pivot
(
    min( SOLD_TO_PARTY_NM ) for HD in (
        [2016-05-13 2016-05-20 AXIS],
        [2016-05-10 2016-05-13 TMEH-J],
        [2016-05-12 2016-05-20 TMEH-J],
        [2016-05-17 2016-05-20 TMEH-J]
     )
) as pt
union

select ITEM_CD, SPEC,
       convert(varchar,[2016-05-13 2016-05-20 AXIS]),
       convert(varchar,[2016-05-10 2016-05-13 TMEH-J]),
       convert(varchar,[2016-05-12 2016-05-20 TMEH-J]),
       convert(varchar,[2016-05-17 2016-05-20 TMEH-J]),
       9999 as odr
from src
pivot
(
    sum( SO_QTY ) for HD in (
        [2016-05-13 2016-05-20 AXIS],
        [2016-05-10 2016-05-13 TMEH-J],
        [2016-05-12 2016-05-20 TMEH-J],
        [2016-05-17 2016-05-20 TMEH-J]
     )
) as pt
 

order by odr