Presto SQL 数据透视(由于缺乏更好的词)数据

2024-04-04

我正在处理 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Presto SQL 数据透视(由于缺乏更好的词)数据 的相关文章

随机推荐