teradata sql pivot multiple occurrences into additional columns

后端 未结 1 1657
情歌与酒
情歌与酒 2020-12-15 11:10

I have something like this:

ID      Result
1       value1
2       value1
2       value2
3       value1
4       value1
4       value2
4       value3


        
相关标签:
1条回答
  • 2020-12-15 11:51

    Unfortunately Teradata doesn't have a PIVOT function but you can use an aggregate function with a CASE expression to get the result.

    select id,
        max(case when seq =1 then result end) result1,
        max(case when seq =2 then result end) result2,
        max(case when seq =3 then result end) result3
    from
    (
        select id, res, row_number() over(partition by id order by result) seq
        from yourtable
    ) d
    group by id
    order by id;
    

    If you have more values for each ID, then you can add more CASE expressions.

    0 讨论(0)
提交回复
热议问题