需要SQL解决方案

时间:2018-08-14 21:19:13

标签: sql sql-server sql-server-2008

问题:

Num  String      Score
1    Overall      5
1    Comments    Good
2    Overall      4
2    Comments    Fine
3    Overall      5
3    Comments    Fine

我想要这样的结果。

Num    String    Score    Comments
1       Overall   5        Good
2       Overall   4        Fine
3       Overall   5        Fine

有人可以为此提供SQL代码吗?

1 个答案:

答案 0 :(得分:2)

这是使用conditional aggregation的一种方法:

select num, 
    'Overall' as string,
    max(case when string = 'Overall' then score end) as score
    max(case when string = 'Comments' then score end) as comments
from yourtable
group by num