Presto SQL透视(因为没有更好的词)数据 [英] Presto SQL pivoting (for lack of a better word) data
本文介绍了Presto SQL透视(因为没有更好的词)数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在处理Presto数据库中的一些课程数据。表中的数据如下所示:
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
我要格式化数据,使其如下所示:
student_id 2018_Q1_score 2018_Q1_completed 2017_Q3_score
1 0 N 5
3 4 Y 4
4 2 N 2
我知道我可以通过连接到每个时间段的表来做到这一点,但我想在这里询问是否有专家推荐更具可伸缩性的解决方案(例如,也许不必为每个时间段手动创建新的连接)。有什么建议吗?
推荐答案
只能使用条件聚合:
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
这篇关于Presto SQL透视(因为没有更好的词)数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文