TSQL pivoting a table without an aggregation
I have this table
name hourOfDay score
bill 1 22
bill 2 28
bill 3 29
bill 4 24
bill 5 34
and would like this table
name 1 2 3 4 5
bill 22 28 29 24 34
开发者_运维技巧Right now I'm doing this with tons of joins, is there a fast way to do this? My table is too large to use joins.
You can use PIVOT
and an aggregation anyway. Try this:
SELECT *
FROM (SELECT name, HourOfDay, Score FROM YourTable) YT
PIVOT(MIN(Score) FOR HourOfDay IN ([1],[2],[3],.....,[24])) AS PT
You can use MIN
or MAX
, since it should be the same result.
精彩评论