简体   繁体   中英

TSQL - Pivot multiple columns

I need help with my query in pivoting multiple columns, basically, I just need a simple query and I can't get my head around it, hope someone can help me.

Unpivoted:

MTH         ID      TEAM    Metric_ID   Score   Outcome_Score
1/10/2016   abc11   teamA   ID_1        292     3
1/11/2016   abc11   teamA   ID_1        300     0
1/10/2016   abc11   teamA   ID_10       100     0
1/11/2016   abc11   teamA   ID_10       84      0
1/10/2016   abc11   teamA   ID_11       11%     0
1/11/2016   abc11   teamA   ID_11       12%     0

TO BE:

LANID   TEAM    Metric_ID   Oct_Score   Oct_Outcome_Score   Nov_Score   Nov_Outcome_Score
abc11   teamA   ID_1        292         3                   300         0
abc11   teamA   ID_10       100         0                   84          0
abc11   teamA   ID_11       11%         0                   12%         0

I have come up with below, but everytime I am adding the field for Outcome, it messes the query altogether. The actual query is more complex. I just need idea. Thanks.

SELECT          ID AS LANID, Team, Metric_ID
                , CONVERT(varchar(3), MTH, 100) SMTH
                , Oct_Score
                , Oct_Outcome_Score
                , Nov_Score
                , Nov_Outcome_Score
FROM            TableA
PIVOT
    (
                SUM(Score) 
                FOR SMTH IN (Oct_Score, Nov_Score)
    ) AS P

This should work:

WITH Src AS
(
    SELECT * FROM (VALUES   
    ('1/10/2016', 'abc11', 'teamA', 'ID_1 ', 292, 3),
    ('1/11/2016', 'abc11', 'teamA', 'ID_1 ', 300, 0),
    ('1/10/2016', 'abc11', 'teamA', 'ID_10', 100, 0),
    ('1/11/2016', 'abc11', 'teamA', 'ID_10', 84, 0),
    ('1/10/2016', 'abc11', 'teamA', 'ID_11', 11, 0),
    ('1/11/2016', 'abc11', 'teamA', 'ID_11', 12, 0)
    )T(MTH, ID, TEAM, Metric_ID, Score, Outcome_Score)
)
SELECT * FROM
(
    SELECT ID, TEAM, Metric_ID, Value,
        CASE WHEN DATEPART(M, MTH)=10 THEN 'Oct_'
             WHEN DATEPART(M, MTH)=11 THEN 'Nov_' END+Col ToPivot
    FROM
    (SELECT CONVERT(date, MTH, 103) MTH, ID, TEAM, Metric_ID, Score, Outcome_Score FROM Src) T1
    UNPIVOT (Value FOR Col IN (Score, Outcome_Score)) U1
) T
PIVOT (SUM(Value) FOR ToPivot IN (Oct_Score, Oct_Outcome_Score, Nov_Score, Nov_Outcome_Score)) P

Result:

ID    TEAM  Metric_ID Oct_Score   Oct_Outcome_Score Nov_Score   Nov_Outcome_Score
----- ----- --------- ----------- ----------------- ----------- -----------
abc11 teamA ID_1      292         3                 300         0
abc11 teamA ID_10     100         0                 84          0
abc11 teamA ID_11     11          0                 12          0

To Dynamic Pivot you may use bellow query (In Dynamic Pivot Column Name will not in proper order)

CREATE Table #Item(id varchar(250),team varchar(250),Metric_ID varchar(20),col varchar(250),Score int)
;WITH Src AS(SELECT * FROM (VALUES   ('10/1/2016', 'abc11', 'teamA', 'ID_1 ', 292, 3),('11/1/2016', 'abc11', 'teamA', 'ID_1 ', 300, 0),('10/1/2016', 'abc11', 'teamA', 'ID_10', 100, 0),('11/1/2016', 'abc11', 'teamA', 'ID_10', 84, 0),('10/1/2016', 'abc11', 'teamA', 'ID_11', 11, 0),('11/1/2016', 'abc11', 'teamA', 'ID_11', 12, 0))T(MTH,ID, TEAM, Metric_ID, Score, Outcome_Score))
INSERT INTO #Item
select ID,TEAM,Metric_ID,DateName(M, DateAdd(M,  MONTH(MTH), -1))+'_Score',Score from Src
UNION ALL
select ID,TEAM,Metric_ID,DateName(M, DateAdd(M,  MONTH(MTH), -1))+'_Outcome_Score',Outcome_Score from Src
DECLARE @Query AS NVARCHAR(MAX) DECLARE @ColumnName AS NVARCHAR(MAX)
SELECT @ColumnName= ISNULL(@ColumnName + ',','') + QUOTENAME(col)
FROM (SELECT DISTINCT col FROM #Item) AS ColName
SET @Query = N'SELECT ID,TEAM,Metric_ID   ,' + @ColumnName + 'FROM #Item PIVOT(SUM(Score) FOR col IN (' + @ColumnName + ')) AS PVTTable'
EXEC sp_executesql @Query
Drop table #Item

Finally, this was resolved. The cause is missing "GROUP BY" hence the data doubled / tripled depending on number of pivoted columns.

For reference, pls refer on this link.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM