如何在没有透视功能的情况下转动表格?

时间:2015-02-12 19:15:52

标签: sql

我需要转动一个表,将行值设置为列,但问题是我没有要调用的数据透视功能。我需要找到一种解决方法来完成这项工作。

示例:

NAME   SUBJECT     MARKS
Adam    maths        88
Adam    Science      76
Matt    Science      87
joe     English      90 
joe     Maths        80 
joe     Science      40  

Needs to look like : 

NAME     SCIENCE   MATHS    ENGLISH   
Adam        76      88        null
Matt        87      null      null
Joe         40      80        90

我没有枢轴功能。

1 个答案:

答案 0 :(得分:3)

如果没有数据透视,您可以在聚合函数中使用case语句获得相同的结果......

select
    name,
    science = sum(case when subject = 'science' then marks else null end)
    maths = sum(case when subject = 'maths' then marks else null end)
from
    table
group by
    name