Presto SQL透视(因为没有更好的词)数据透视、数据、Presto、SQL

由网友(22.清欢渡劫)分享简介:我正在处理Presto数据库中的一些课程数据。表中的数据如下所示:student_id period score completed1 2016_Q1 3 Y1 2016_Q3 4 Y3 2017_Q1 4 Y4...

我正在处理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
prestosql和prestodb对hudi的支持分析

我知道我可以通过连接到每个时间段的表来做到这一点,但我想在这里询问是否有专家推荐更具可伸缩性的解决方案(例如,也许不必为每个时间段手动创建新的连接)。有什么建议吗?

推荐答案

只能使用条件聚合:

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
阅读全文

相关推荐

最新文章