我想将列转换为行

时间:2013-10-10 10:07:58

标签: postgresql amazon-redshift

我的数据在redshift sql server中如下:

MatchId TeamId  Teamname  Home/away  Teamstats  statsvalue
1          101     a          home      yards      0
1          101     a          home      firstdown  1
1          101     a          home      points     2
1          101     a          home      completion 4
1          202     b          away      sacks      3
1          202     b          away      penalties  5
1          202     b          away      yards      6
1          202     b          away      points     7

我希望数据如下:

MatchId TeamId  Teamname  Home/away  yards  firstdown points completion sacks penalties 
1          101     a          home     0       1        2        4        3       5
1          202     b          away     6       null     7         null    null    null

1 个答案:

答案 0 :(得分:1)

然后你需要一个pivot

Select *
from yourtable
pivot (max(statsvalue) for teamstats in 
     (yards, firstdown, points, completion, sacks, penalties) 
) p