开发者

TSQL pivoting a table without an aggregation

开发者 https://www.devze.com 2023-03-22 00:50 出处:网络
I have this table namehourOfDayscore bill122 bill228 bill329 bill424 bill534 and would like this table name12345

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消