Hive 刷题—— 每年的在校人数
问题描述
year表示学生入学年度,num表示对应年度录取学生人数,stu_len表示录取学生的学制;说明:例如录取年度2018学制是3年,表示该批学生在校年份为2018~2019、2019~2020、2020-2021,在算每年的在校人数时,2018/2019/2020/2021年份都需要算上。
示例数据
id year num stu_len 1 2018 2000 3 2 2019 2000 3 3 2020 1000 4 3 2020 2000 3
参考实现
with temp as (select 2018 as year, 3 as stu_len, 2000 as num union all select 2019 as year, 3 as stu_len, 2000 as num union all select 2020 as year, 4 as stu_len, 1000 as num union all select 2020 as year, 3 as stu_len, 2000 as num) select t.year,sum(t1.num) num from (select min_year + pos as year from (select min(year) min_year, max(year + stu_len) max_year from temp) t lateral view posexplode(split(repeat(',', max_year - min_year), ',')) tbl as pos, val) t join temp t1 on t.year between t1.year and t1.year + t1.stu_len group by t.year order by 1;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
2023-03-29 奇安信 软件卸载
2022-03-29 kafka 生产者(一)