当前位置: 首页 > article >正文

hive-sql 计算每年在校生人数

sql:

用到函数 space() :生产空格 split 切分空格 

posexplode 炸裂函数 返回下标和具体指

with data as (
--             主键        入学年份            人数            学制
    select 1 as id , 2001 as yy , 1200 as stu_num , '3' as stu_len union all
    select 2 as id , 2000 as yy , 1300 as stu_num , '5' as stu_len union all
    select 3 as id , 2003 as yy , 1400 as stu_num , '4' as stu_len union all
    select 4 as id , 2002 as yy , 1500 as stu_num , '3' as stu_len union all
    select 5 as id , 2002 as yy , 1600 as stu_num , '4' as stu_len union all
    select 6 as id , 2014 as yy , 1600 as stu_num , '4' as stu_len union all
    select 7 as id , 2015 as yy , 1600 as stu_num , '4' as stu_len union all
    select 8 as id , 2015 as yy , 1600 as stu_num , '4' as stu_len union all
    select 9 as id , 2015 as yy , 1600 as stu_num , '4' as stu_len union all
    select 10 as id , 2015 as yy , 1600 as stu_num , '4' as stu_len union all
    select 11 as id , 2016 as yy , 1600 as stu_num , '4' as stu_len union all
    select 12 as id , 2016 as yy , 1600 as stu_num , '4' as stu_len union all
    select 13 as id , 2016 as yy , 1600 as stu_num , '4' as stu_len union all
    select 14 as id , 2016 as yy , 1600 as stu_num , '4' as stu_len union all
    select 15 as id , 2016 as yy , 1600 as stu_num , '4' as stu_len 
)
SELECT
  endyy,
  SUM(stu_num)
FROM
  (
    SELECT
      id,
      (yy + pos) AS endyy,
      stu_num,
      stu_len,
      val
    FROM
      data LATERAL view posexplode(split(space(CAST(stu_len AS INT) -1),' ')) t AS pos,
      val
  ) t
GROUP BY
  endyy

结果:


http://www.kler.cn/a/461124.html

相关文章:

  • 【可实战】需求分析-测试计划↓-测试设计-测试执行-测试总结↓(包含测试计划、测试总结模板,以公司要求为准)
  • Linux中隐藏操作身法
  • C# 服务调用RFC函数获取物料信息,并输出生成Excel文件
  • kubernetes学习-kubectl命令、探针(二)
  • uni-app 多平台分享实现指南
  • vue中的设计模式
  • 写在2024的最后一天
  • 【浏览器】缓存
  • Android 检测设备是否 Root
  • 【数据结构】线性数据结构——栈
  • 本地部署Hello-Algo打造私人算法教练让算法学习告别网络限制
  • 解构大语言模型(LLM)
  • 如何免费解锁 IPhone 网络
  • 如何使用 ChatGPT Prompts 写学术论文?
  • 嵌入式单片机中SPI外设控制与实现
  • 网神SecFox运维安全管理与审计系统 /authService/login接口反序列化漏洞复现 [附POC]
  • Vue.js组件开发-实现多级菜单
  • want php学习笔记
  • 【mysql】linux安装mysql客户端
  • 计算机体系结构期末考试
  • PDF怎么压缩得又小又清晰?5种PDF压缩方法
  • WPF合并C1FlexGrid表格,根据多列的值进行合并
  • JavaWeb开发(二)IDEA创建Java Web项目并部署及目录结构
  • Applied Spatial Statistics(十三)带有空间平滑器的 GAM
  • 批量新建工作表,带个性化模板一步到位-Excel易用宝
  • 12.29~12.31[net][review]need to recite[part 2]