A 테이블
Seq |
Team |
Divi |
Detail |
2 |
광학팀 |
Delivery |
Laser |
9 |
광학팀 |
Delivery |
Chiller |
10 |
광학팀 |
Delivery |
BET |
11 |
광학팀 |
Delivery |
BET Mount |
12 |
광학팀 |
Delivery |
ATT |
13 |
광학팀 |
Delivery |
Wave Plate |
14 |
광학팀 |
Delivery |
POL |
15 |
광학팀 |
Delivery |
Mirror 종류 |
16 |
광학팀 |
Delivery |
Mirror Spring |
17 |
광학팀 |
Head |
Shutter 위치 |
18 |
광학팀 |
Head |
Power Meter |
21 |
광학팀 |
Head |
LPC |
22 |
광학팀 |
Head |
TC Lens |
23 |
광학팀 |
Head |
Lens Mounting 방법 |
B 테이블
Seq |
yyyy |
OrderNo |
CustNm |
1 |
2018 |
18A765 |
UXN |
2 |
2019 |
20B001 |
비에이치 |
3 |
2019 |
20B002 |
비에이치 |
4 |
2019 |
20B003 |
비에이치 |
5 |
2019 |
20B004 |
비에이치 |
6 |
2019 |
20A141 |
비에이치 |
7 |
2019 |
20A142 |
비에이치 |
8 |
2019 |
20B010 |
비에이치 |
9 |
2019 |
20B011 |
비에이치 |
C 테이블 (idx 컬럼 추가 - A테이블 Seq 값)
Seq |
idx |
Team |
Divi |
Detail |
OrderNo |
Result |
1 |
2 |
광학팀 |
Delivery |
Laser |
20B001 |
1 |
2 |
2 |
광학팀 |
Delivery |
Chiller |
20B001 |
2 |
3 |
2 |
광학팀 |
Delivery |
BET |
20B001 |
3 |
4 |
2 |
광학팀 |
Delivery |
BET Mount |
20B001 |
4 |
5 |
2 |
광학팀 |
Delivery |
ATT |
20B001 |
5 |
6 |
2 |
광학팀 |
Delivery |
Wave Plate |
20B001 |
6 |
7 |
2 |
광학팀 |
Delivery |
POL |
20B001 |
7 |
8 |
2 |
광학팀 |
Delivery |
Mirror 종류 |
20B001 |
8 |
9 |
2 |
광학팀 |
Delivery |
Mirror Spring |
20B001 |
9 |
10 |
2 |
광학팀 |
Head |
Shutter 위치 |
20B001 |
10 |
11 |
2 |
광학팀 |
Head |
Power Meter |
20B001 |
11 |
12 |
2 |
광학팀 |
Head |
LPC |
20B001 |
12 |
13 |
2 |
광학팀 |
Head |
TC Lens |
20B001 |
13 |
14 |
2 |
광학팀 |
Head |
Lens Mounting 방법 |
20B001 |
14 |
15 |
2 |
광학팀 |
Delivery |
Laser |
20B004 |
1 |
여기서 어떤식으로 변경해야 하나요??
Declare @Columns Varchar(max), @SQL Varchar(max), @SQL1 Varchar(max)
Set @Columns = ''
Select @Columns = @Columns + '[' + Convert(varchar(10),Seq) + '],'
From (
Select Seq From SPSSCOrderNo
) AS ods
Set @Columns = LEFT(@Columns, LEN(@Columns) - 1)
------------
Set @SQL = ''
Set @SQL1 = ''
SET @SQL = '
SELECT CASE Detail WHEN ''OrderNo'' THEN 0 ELSE 1 END seq
, a.*
FROM (SELECT Seq
, CAST(OrderNo AS VARCHAR(6)) OrderNo
, CAST(CustNm AS VARCHAR(6)) CustNm
, '''' Team
, '''' Divi
FROM SPSSCTitle
) a
UNPIVOT (v FOR Detail IN (OrderNo, CustNm)) a
PIVOT (MIN(v) FOR Seq IN (' + @Columns + ')) a
UNION
SELECT *
FROM (SELECT c.Seq
, c.Team
, c.Divi
, c.Detail
, c.Result
, b.Seq AS Seq_b
FROM SPSSCTitle b
INNER JOIN SPSSCDetailOrder c
ON b.OrderNo = c.OrderNo
) a
PIVOT (MIN(Result) FOR Seq_b IN (' + @Columns + ')) a
'
EXEC(@SQL)
최종 원한는 화면
(동적) | |||||||||||||||
번호 | 18A765 | 20B001 | 20B002 | 20B003 | 20B004 | 20A141 | 20A142 | 20B010 | 20B011 | 21A001 | 21A002 | 21A003 | …n | ||
NO | |||||||||||||||
Team | Divi | Detail | |||||||||||||
광학팀 | Delivery | Laser | 1 | 1 | |||||||||||
광학팀 | Delivery | Chiller | 2 | ||||||||||||
광학팀 | Delivery | BET | 3 | ||||||||||||
광학팀 | Delivery | BET Mount | 4 | ||||||||||||
광학팀 | Delivery | ATT | 5 | ||||||||||||
광학팀 | Delivery | Wave Plate | 6 | ||||||||||||
광학팀 | Delivery | POL | 7 | ||||||||||||
광학팀 | Delivery | Mirror 종류 | 8 | ||||||||||||
광학팀 | Delivery | Mirror Spring | 9 | ||||||||||||
광학팀 | Head | Shutter 위치 | 10 | ||||||||||||
광학팀 | Head | Power Meter | 11 |
-- MSSQL --
WITH
tab_b
AS
(
SELECT
1 Seq,
'2018'
yyyy,
'18A765'
OrderNo,
'A'
CustNm
UNION
ALL
SELECT
2,
'2019'
,
'20B001'
,
'A'
UNION
ALL
SELECT
3,
'2019'
,
'20B002'
,
'A'
UNION
ALL
SELECT
4,
'2019'
,
'20B003'
,
'B'
UNION
ALL
SELECT
5,
'2019'
,
'20B004'
,
'B'
UNION
ALL
SELECT
6,
'2019'
,
'20A141'
,
'B'
UNION
ALL
SELECT
7,
'2019'
,
'20A142'
,
'B'
UNION
ALL
SELECT
8,
'2019'
,
'20B010'
,
'B'
UNION
ALL
SELECT
9,
'2019'
,
'20B011'
,
'D'
UNION
ALL
SELECT
10,
'2021'
,
'21A001'
,
'D'
UNION
ALL
SELECT
11,
'2021'
,
'21A002'
,
'D'
UNION
ALL
SELECT
12,
'2021'
,
'21A003'
,
'D'
)
, tab_c
AS
(
SELECT
2 Seq,
'광학팀'
Team,
'Delivery'
Divi,
'Laser'
Detail,
'20B001'
OrderNo,
null
Result
UNION
ALL
SELECT
9,
'광학팀'
,
'Delivery'
,
'Chiller'
,
'20B001'
,
null
UNION
ALL
SELECT
10,
'광학팀'
,
'Delivery'
,
'BET'
,
'20B001'
,
'O'
UNION
ALL
SELECT
11,
'광학팀'
,
'Delivery'
,
'BET Mount'
,
'20B001'
,
'O'
UNION
ALL
SELECT
12,
'광학팀'
,
'Delivery'
,
'ATT'
,
'20B001'
,
'X'
UNION
ALL
SELECT
13,
'광학팀'
,
'Delivery'
,
'Wave Plate'
,
'20B001'
,
null
UNION
ALL
SELECT
14,
'광학팀'
,
'Delivery'
,
'POL'
,
'20B001'
,
null
UNION
ALL
SELECT
15,
'광학팀'
,
'Delivery'
,
'Mirror 종류'
,
'20B001'
,
null
UNION
ALL
SELECT
16,
'광학팀'
,
'Delivery'
,
'Mirror Spring'
,
'20B001'
,
null
UNION
ALL
SELECT
17,
'광학팀'
,
'Head'
,
'Shutter 위치'
,
'20B001'
,
null
UNION
ALL
SELECT
18,
'광학팀'
,
'Head'
,
'Power Meter'
,
'20B001'
,
null
UNION ALL SELECT 19, '광학팀', 'Delivery', 'BET' , '20B004', 'O'
)
SELECT
CASE
Detail
WHEN
'OrderNo'
THEN
0
ELSE
1
END
seq
, a.*
FROM
(
SELECT
Seq
,
CAST
(OrderNo
AS
VARCHAR
(6)) OrderNo
,
CAST
(CustNm
AS
VARCHAR
(6)) CustNm
,
''
Team
,
''
Divi
FROM
tab_b
) a
UNPIVOT (v
FOR
Detail
IN
(OrderNo, CustNm)) a
PIVOT (
MIN
(v)
FOR
Seq
IN
([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])) a
UNION
SELECT
*
FROM
(
SELECT
c.Seq
, c.Team
, c.Divi
, c.Detail
, c.Result
, b.Seq Seq_b
FROM
tab_b b
INNER
JOIN
tab_c c
ON
b.OrderNo = c.OrderNo
) a
PIVOT (
MIN
(Result)
FOR
Seq_b
IN
([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])) a
;
현재이렇게 출력됨
원하는 출력형태
피벗팅 했던 쿼리에 group by를 한번 더 하시면 될 것 같습니다.
그리고 seq=19의 결과가 10번 줄로 합해져야 하기 때문에 seq = 19는 사라질 것 같습니다.
쿼리 참고하십시오.
WITH tab_b AS
(
SELECT 1 Seq, '2018' yyyy, '18A765' OrderNo, 'A' CustNm
UNION ALL SELECT 2, '2019', '20B001', 'A'
UNION ALL SELECT 3, '2019', '20B002', 'A'
UNION ALL SELECT 4, '2019', '20B003', 'B'
UNION ALL SELECT 5, '2019', '20B004', 'B'
UNION ALL SELECT 6, '2019', '20A141', 'B'
UNION ALL SELECT 7, '2019', '20A142', 'B'
UNION ALL SELECT 8, '2019', '20B010', 'B'
UNION ALL SELECT 9, '2019', '20B011', 'D'
UNION ALL SELECT 10, '2021', '21A001', 'D'
UNION ALL SELECT 11, '2021', '21A002', 'D'
UNION ALL SELECT 12, '2021', '21A003', 'D'
)
, tab_c AS
(
SELECT 2 Seq, '광학팀' Team, 'Delivery' Divi, 'Laser' Detail, '20B001' OrderNo, null Result
UNION ALL SELECT 9, '광학팀', 'Delivery', 'Chiller' , '20B001', null
UNION ALL SELECT 10, '광학팀', 'Delivery', 'BET' , '20B001', 'O'
UNION ALL SELECT 11, '광학팀', 'Delivery', 'BET Mount' , '20B001', 'O'
UNION ALL SELECT 12, '광학팀', 'Delivery', 'ATT' , '20B001', 'X'
UNION ALL SELECT 13, '광학팀', 'Delivery', 'Wave Plate' , '20B001', null
UNION ALL SELECT 14, '광학팀', 'Delivery', 'POL' , '20B001', null
UNION ALL SELECT 15, '광학팀', 'Delivery', 'Mirror 종류' , '20B001', null
UNION ALL SELECT 16, '광학팀', 'Delivery', 'Mirror Spring', '20B001', null
UNION ALL SELECT 17, '광학팀', 'Head' , 'Shutter 위치' , '20B001', null
UNION ALL SELECT 18, '광학팀', 'Head' , 'Power Meter' , '20B001', null
UNION ALL SELECT 19, '광학팀', 'Delivery', 'BET' , '20B004', 'O'
)
SELECT CASE Detail WHEN 'OrderNo' THEN 0 ELSE 1 END seq
, a.*
FROM (SELECT Seq
, CAST(OrderNo AS VARCHAR(6)) OrderNo
, CAST(CustNm AS VARCHAR(6)) CustNm
, '' Team
, '' Divi
FROM tab_b
) a
UNPIVOT (v FOR Detail IN (OrderNo, CustNm)) a
PIVOT (MIN(v) FOR Seq IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])) a
UNION
select
min(seq) as seq ,
team,
divi,
detail,
max([1]) as [1] ,
max([2]) as [2] ,
max([3]) as [3] ,
max([4]) as [4] ,
max([5]) as [5] ,
max([6]) as [6] ,
max([7]) as [7] ,
max([8]) as [8] ,
max([9]) as [9] ,
max([10]) as [10] ,
max([11]) as [11] ,
max([12]) as [12]
from (
SELECT
*
FROM (SELECT c.Seq
, c.Team
, c.Divi
, c.Detail
, c.Result
, b.Seq Seq_b
FROM tab_b b
INNER JOIN tab_c c
ON b.OrderNo = c.OrderNo
) a
PIVOT (MIN(Result) FOR Seq_b IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])) a
) p
group by team, divi, detail
order by seq
;