我有一张包含这些信息的表格:

code    score    quality
123     2015      12
123     2016      16
123     2017      14

我想这样展示
  code    2015    2016  2017
    123    12      16    14

你能帮我吗?谢谢

最佳答案

一种方法是使用条件聚合:

select code,
    max(case when score = 2015 then quality end) as [2015],
    max(case when score = 2016 then quality end) as [2016],
    max(case when score = 2017 then quality end) as [2017]
from your_table
group by code;

Demo

或使用 PIVOT :
select *
from your_table
pivot (
    max(quality) for score in ([2015],[2016],[2017])
) p;

Demo

关于sql - 如何使用 Cube、Pivo​​t 或 Rollup SQL 转换列中的行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43943876/

10-12 00:05
查看更多