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

hive开窗函数

hive开窗函数

窗口函数

数据准备

1 jx 20
2 zx 24
3 yx 18
4 wz 10
5 yy 34
6 wy 25
create table t (> id int,> name string,> age int> )> row format delimited fields terminated by ' '; 
load data inpath '/data/data.txt' into table t;

在这里插入图片描述

ROW_NUMBER

ROW_NUMBER 从1开始,生成分组内记录的数据

select id, name, age, row_number() over(order by age desc) num from t; 

结果展示

id	name	age	num
5	yy	34	1
6	wy	25	2
2	zx	24	3
1	jx	20	4
3	yx	18	5
4	wz	10	6

RANK 和 DENSE_RANK

RANK生成数据在分组中的排名,排名相等的会在名次中留下空位

DENSE_RANK 生成数据在分组中的排名,排名相等的不会留下空位

select 
id, name, age,
rank() over(order by age desc) num1,
dense_rank() over(order by age desc) num2,
row_number() over(order by age desc) num3
from t;

结果展示(中途插入数据忘记覆盖原数据了,但是不影响展示结果)

id	name	age	num1	num2	num3
5	yy	34	1	1	1
5	yy	34	1	1	2
6	wy	25	3	2	3
6	wy	25	3	2	4
2	zx	24	5	3	5
2	zx	24	5	3	6
1	jx	20	7	4	7
7	hn	20	7	4	8
1	jx	20	7	4	9
3	yx	18	10	5	10
3	yx	18	10	5	11
4	wz	10	12	6	12
4	wz	10	12	6	13

分析窗口函数

SUM

结果和order by相关,默认为升序

 select id,name,age,sum(age)over(order by age) sum from t;

结果展示

id	name	age	sum
4	wz	10	10
3	yx	18	28
1	jx	20	68
7	hn	20	68
2	zx	24	92
6	wy	25	117
5	yy	34	151

如果没有orger by 则默认将分区内所有的数据进行sum

select id,name,age,sum(age)over() sum from t;

结果展示

id	name	age	sum
1	jx	20	151
2	zx	24	151
3	yx	18	151
4	wz	10	151
5	yy	34	151
6	wy	25	151
7	hn	20	151

如果不指定rows between,默认从起点到当前行

rows between的含义

  • preceding : 往前
  • following : 往后
  • current row : 当前行
  • unbounded : 起点
  • unbounded preceding : 默认从前面的起点
  • unbounded following : 默认到后面的终点

从起点到终点进行sum

select id, name, age, sum(age)over(order by age rows between unbounded preceding and current row) sum from t;

结果展示

id	name	age	sum
4	wz	10	10
3	yx	18	28
1	jx	20	48
7	hn	20	68
2	zx	24	92
6	wy	25	117
5	yy	34	151

对前三行和本行和下一行进行sum

select id, name, age, sum(age)over(order by age rows between 3 preceding and 1 following) sum from t;

结果展示

id	name	age	sum
4	wz	10	28
3	yx	18	48
1	jx	20	68
7	hn	20	92
2	zx	24	107
6	wy	25	123
5	yy	34	103

对当前行到终点进行sum

select id, name, age, sum(age)over(order by age rows between current row and unbounded following) sum from t;

结果展示

id	name	age	sum
4	wz	10	151
3	yx	18	141
1	jx	20	123
7	hn	20	103
2	zx	24	83
6	wy	25	59
5	yy	34	34

其余还有avg、min、max和sum的用法一样

这里只再展示一个avg

 select id,name,age,avg(age)over(order by age) sum from t;

结果展示

id	name	age	sum
4	wz	10	10.0
3	yx	18	14.0
1	jx	20	17.0
7	hn	20	17.0
2	zx	24	18.4
6	wy	25	19.5
5	yy	34	21.571428571428573
http://www.lryc.cn/news/6937.html

相关文章:

  • 安全多方计算系列笔记1——前世今生
  • 16- 梯度提升分类树GBDT (梯度下降优化) (算法)
  • SpringCloud+Nacos+Gateway
  • 高通开发系列 - linux kernel内核升级msm-3.18升至msm-4.9(2)
  • Spring依赖注入与反转控制到底是个啥?
  • Linux Shell脚本讲解
  • Linux:用户空间非法指针coredump简析
  • 带你玩转Jetson之Deepstream简明教程(四)DeepstreamApp如何使用以及用于工程验证。
  • 快速搭建个人在线书库,随时随地畅享阅读!
  • 电子纸墨水屏的现实应用场景
  • 常量const、引用、指针的大杂烩
  • 宝塔搭建实战php开源likeadmin通用管理移动端uniapp源码(四)
  • Hive的分区表与分桶表内部表外部表
  • 和数集团打造《神念无界:源起山海》,诠释链游领域创新与责任
  • 小白入门模拟IC设计,如何快速学习?
  • 51单片机——中断系统之外部中断实验,小白讲解,相互学习
  • 如何设计一个秒杀系统
  • 厄瓜多尔公司注册方案
  • 安全渗透环境准备(工具下载)
  • 118.(leaflet篇)leaflet空间判断-点与geojson面图层的空间关系(turf实现)
  • 目标检测与目标跟踪算法技术汇总
  • Linux 系统启动过程
  • 【每日一题Day118】LC1124表现良好的最长时间段 | 前缀和+单调栈/哈希表
  • vue使用nprogress(进度条)
  • @NotNull 、@NotBlank、@NotEmpty区别和使用
  • Nacos——Nacos简介以及Nacos Server安装
  • Presto 文档和笔记
  • 大尺度衰落与小尺度衰落
  • 完美解决:重新安装VMware Tools灰色。以及共享文件夹的创建(centos8)
  • 达梦数据库作业管理