一、每个学科的成绩第一名是谁?
0 问题描述
基于学生成绩表输出每个科目的第一名是谁呢?
1 数据准备
with t1 as(select'zs' as name,'[{"Chinese":80},{"Math":70}],{"English":60}]' as score_extunion allselect'ls' as name,'[{"Chinese":90},{"Math":70}],{"English":90}]' as score_extunion allselect'ww' as name,'[{"Chinese":60},{"Math":90}],{"English":80}]' as score_ext),t2 as(selectname,-- 需要把 [] " {} 都给去掉,方面后面操作regexp_replace(score_ext, '\\[|\\{|\\}|\\"|\\]', '') as scoresfrom t1),t3 as (selectname,split(score, ":")[0] as course,split(score, ":")[1] as scorefrom t2lateral view explode(split(scores, ',')) expl as score)selectname,course,score
from (selectcourse,name,score,row_number() over (partition by course order by score desc) as rnfrom t3) t4
where rn = 1;
2 数据分析
3 小结
该题目利用(行转列)炸裂+开窗进行 解决;