Re: Ordering the columns in a pivot table

7 views
Skip to first unread message

Erland Sommarskog

unread,
Apr 12, 2020, 7:59:11 AM4/12/20
to
SELECT PE.TestID,
MIN(CASE PU.PresentOrder WHEN 1 THEN PE.TextValue END) AS Param1,
MIN(CASE PU.PresentOrder WHEN 2 THEN PE.TextValue END) AS Param2,
MIN(CASE PU.PresentOrder WHEN 3 THEN PE.TextValue END) AS Param3,
MIN(CASE PU.PresentOrder WHEN 4 THEN PE.TextValue END) AS Param4,
MIN(CASE PU.PresentOrder WHEN 5 THEN PE.TextValue END) AS Param5
FROM ParametersEntries PE
JOIN ParametersUser PU ON PE.TestID = PU.TestID
GROUP BY PE.TestID

You may note that I don't use the PIVOT keyward. The above method with CASE
is a lot more flexible and easier to understand.
Reply all
Reply to author
Forward
0 new messages