如何在没有透视功能的情况下透视表

  • 本文关键字:透视 功能 情况下 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

我没有可用的枢轴功能。

在没有pivot的情况下,您可以在聚合函数中使用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

最新更新