Presto SQL pivoting (for lack of a better word) data

aguadamuz picture aguadamuz · Jul 17, 2018 · Viewed 8.7k times · Source

I am working with some course data in a Presto database. The data in the table looks like:

student_id  period   score completed
1           2016_Q1  3     Y
1           2016_Q3  4     Y
3           2017_Q1  4     Y
4           2018_Q1  2     N

I would like to format the data so that it looks like:

student_id  2018_Q1_score 2018_Q1_completed 2017_Q3_score
1           0             N                 5
3           4             Y                 4
4           2             N                 2

I know that I could do this by joining to the table for each time period, but I wanted to ask here to see if any gurus had a recommendation for a more scalable solution (e.g. perhaps not having to manually create a new join for each period). Any suggestions?

Answer

Gordon Linoff picture Gordon Linoff · Jul 17, 2018

You can just use conditional aggregation:

select student_id,
       max(case when period = '2018_Q1' then score else 0 end) as score_2018q1,
       max(case when period = '2018_Q1' then completed then 'N' end) as completed_2018q1,
       max(case when period = '2017_Q3' then score else 0 end) as score_2017q3
from t
group by student_id