在
[SQL] PIVOT 和 UNPIVOT 內有人詢問,在不確定轉置選項的情況下,能否使用 PIVOT 來滿足轉置需求。
利用動態組字串方式來達到此需求。
CREATE TABLE #PIVOT (Employee char(8),Date datetime,Kind varchar(10),Hours numeric(4,2))
INSERT INTO #PIVOT VALUES ('AAAAA','20101001','事假',3.0)
INSERT INTO #PIVOT VALUES ('AAAAA','20101002','事假',8.0)
INSERT INTO #PIVOT VALUES ('AAAAA','20101003','事假',4.0)
INSERT INTO #PIVOT VALUES ('AAAAA','20101010','病假',8.0)
INSERT INTO #PIVOT VALUES ('AAAAA','20101020','病假',1.0)
INSERT INTO #PIVOT VALUES ('AAAAA','20101025','公假',8.0)
INSERT INTO #PIVOT VALUES ('AAAAA','20101026','公假',8.0)
INSERT INTO #PIVOT VALUES ('AAAAA','20101027','公假',8.0)
INSERT INTO #PIVOT VALUES ('AAAAA','20101027','公假',NULL)
INSERT INTO #PIVOT VALUES ('BBBBB','20101001','事假',6.0)
INSERT INTO #PIVOT VALUES ('BBBBB','20101009','病假',8.0)
INSERT INTO #PIVOT VALUES ('BBBBB','20101019','病假',8.0)
INSERT INTO #PIVOT VALUES ('BBBBB','20101019','病假',NULL)
INSERT INTO #PIVOT VALUES ('BBBBB','20101025','陪產假',8.0)
INSERT INTO #PIVOT VALUES ('BBBBB','20101026','陪產假',8.0)
INSERT INTO #PIVOT VALUES ('BBBBB','20101027','陪產假',8.0)
DECLARE
@pivotcol nvarchar (4000) , -- 轉置欄位變數
@selectCol nvarchar(4000) , -- PIVOT 後要把 NULL 資料轉為 0 用
@tsql1 nvarchar(4000) , -- 動態組出的 T-SQL 語法(資料 NULL 呈現)
@tsql2 nvarchar(4000) -- 動態組出的 T-SQL 語法(資料 0 呈現)
-- 把 Kind 資料塞進 @pivotcol 和 @selectcol 變數內
SELECT
@pivotcol = ISNULL(@pivotcol + ' , ' , '') + QUOTENAME(Kind) ,
@selectcol = ISNULL(@selectcol + ' , ' , '') + 'ISNULL(' + QUOTENAME(Kind) + ',0) AS ' + QUOTENAME(Kind)
FROM #PIVOT
GROUP BY Kind
-- 方法一:資料 NULL 呈現
SET @tsql1 =
'
SELECT *
FROM
(
SELECT Employee,Kind,Hours
FROM #PIVOT
) AS P
PIVOT
(
SUM(Hours) FOR Kind IN (' + @pivotcol + ')
) AS PV
'
-- 方法二:資料 0 呈現
SET @tsql2 =
'
SELECT Employee , ' + @selectcol + '
FROM
(
SELECT Employee,Kind,Hours
FROM #PIVOT
) AS P
PIVOT
(
SUM(Hours) FOR Kind IN (' + @pivotcol + ')
) AS PV
'
EXEC sp_executesql @tsql1
EXEC sp_executesql @tsql2