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

sql:

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

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

sql 复制代码
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

结果:

相关推荐
Rk..2 小时前
数据库索引相关总结
java·数据库·sql
数据的世界012 小时前
SQL使用视图
数据库·sql
B站计算机毕业设计超人4 小时前
计算机毕业设计Python+Vue.js游戏推荐系统 Steam游戏推荐系统 Django Flask 游 戏可视化 游戏数据分析 游戏大数据 爬虫
大数据·hadoop·算法·机器学习·spark·网络爬虫·数据可视化
flying robot4 小时前
Hadoop、Flink、Spark和Kafka
hadoop·flink·spark
栗筝i6 小时前
使用 Docker 安装 Redis
数据库·sql·spring
熟透的蜗牛6 小时前
大数据技术-Hadoop(四)Yarn的介绍与使用
大数据·hadoop·分布式
繁星璀璨G7 小时前
SQL 基础教程 - SQL ORDER BY 关键字
数据库·sql·order by
serve the people7 小时前
腾讯云日志服务根据网段过滤非法数据
sql·云计算·腾讯云
掐死你滴温柔13 小时前
SQLALchemy如何将SQL语句编译为特定数据库方言
数据结构·数据库·python·sql
幽兰的天空15 小时前
Servlet中配置和使用过滤器
hive·hadoop·servlet