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;
复制代码
posted @   晓枫的春天  阅读(25)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
历史上的今天:
2023-03-29 奇安信 软件卸载
2022-03-29 kafka 生产者(一)
点击右上角即可分享
微信分享提示