前言:
之前有和大家分享使用CASE WHEN 搭配聚合函數實現樞紐分析
但今天如果我們要轉換成行的列希望是動態依照目前資料庫的欄位要處理呢?
我們可以使用Dynamic pivot
``Dynamic pivot 核心概念其實是把我們要使用的
pivot SQL`語法動態產生出來
程式碼
CREATE TABLE T(
userName VARCHAR(100),
Price int,
Dt DATE
);
INSERT INTO T VALUES ('Tom',100,'2017-01-01');
INSERT INTO T VALUES ('Amy',200,'2017-01-02');
INSERT INTO T VALUES ('Tom',1311,'2017-01-03');
INSERT INTO T VALUES ('Tom',122,'2017-03-01');
INSERT INTO T VALUES ('Tom',111,'2017-04-01');
INSERT INTO T VALUES ('Amy',232,'2017-05-01');
INSERT INTO T VALUES ('Tom',2312,'2017-05-02');
INSERT INTO T VALUES ('Tom',23,'2017-05-03');
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX);
SET @SQL = STUFF((SELECT distinct ',SUM(CASE WHEN Dt = '''+ CAST(Dt AS VARCHAR(10)) +''' THEN Price ELSE 0 END) AS ' + QUOTENAME(Dt)
FROM T
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)')
,1,1,'');
SET @query = 'SELECT userName,'+@cols+' FROM T GROUP BY userName' ;
EXECUTE sp_executesql @query
因為範例我們使用 SQL SERVER
所以使用 FOR XML PATH 語法將我們** CASE WHEN pivot SQL **語法產生並把他附值給 @cols
變數
SET @cols = STUFF((SELECT distinct ',SUM(CASE WHEN Dt = '''+ CAST(Dt AS VARCHAR(10)) +''' THEN Price ELSE 0 END) AS ' + QUOTENAME(Dt)
FROM T
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)')
,1,1,'')