Accomplish pivot in teradata sql

前端 未结 3 1246
滥情空心
滥情空心 2021-01-03 11:20

Say I have a query that returns values like this:

id    type     value
aaa   1a        10
aaa   1b        20
aaa   1c        7
bbb   2a        10
bbb   1a            


        
3条回答
  •  南笙
    南笙 (楼主)
    2021-01-03 11:51

    Using PIVOT function in Teradata 16 it could look like this (assuming your types are in a table called mytypetable):

    SELECT 
      *
    FROM 
      mytable PIVOT (SUM("value") FOR "type" IN (SELECT "Type" FROM mytypetable)) AS Temp_pivot
    ORDER BY 
      id
    

    One drawback is that you cannot decide on the order of the columns though.

提交回复
热议问题