大数据之Hive:Hive 开窗函数(一)

1.什么是开窗函数?

普通的聚合函数聚合的行集是组,开窗函数聚合的行集是窗口。因此,普通的聚合函数每组(Group by)只返回一个值,而开窗函数则可为窗口中的每行都返回一个值。简单理解,就是对查询的结果多出一列,这一列可以是聚合值,也可以是排序值。

开窗函数一般分为两类,聚合开窗函数和排序开窗函数。

2.聚合开窗函数

聚合开窗函数:有count开窗函数,sum开窗函数,avg开窗函数,min开窗函数,max开窗函数,first_value开窗函数,last_value开窗函数,lag开窗函数,lead开窗函数,cume_dist开窗函数等等;
虽然聚合开窗函数,品类繁多,但本质都是一样的,受篇幅限制,只能重点说几个,入选的理由,一是大家可以通过它,触类旁通,二是因为陌生,所以更应该熟悉。
综上:着重说明一下count开窗函数,first_value开窗函数,lag开窗函数和cume_dist开窗函数等;

2.1 count开窗函数

语义:求个数(行数)
数据准备:

-- 建表
create table student_scores(
id int,
studentId int,
language int,
math int,
english int,
classId string,
departmentId string
);
-- 写入数据
insert into table student_scores values 
  (1,111,68,69,90,'class1','department1'),
  (2,112,73,80,96,'class1','department1'),
  (3,113,90,74,75,'class1','department1'),
  (4,114,89,94,93,'class1','department1'),
  (5,115,99,93,89,'class1','department1'),
  (6,121,96,74,79,'class2','department1'),
  (7,122,89,86,85,'class2','department1'),
  (8,123,70,78,61,'class2','department1'),
  (9,124,76,70,76,'class2','department1'),
  (10,211,89,93,60,'class1','department2'),
  (11,212,76,83,75,'class1','department2'),
  (12,213,71,94,90,'class1','department2'),
  (13,214,94,94,66,'class1','department2'),
  (14,215,84,82,73,'class1','department2'),
  (15,216,85,74,93,'class1','department2'),
  (16,221,77,99,61,'class2','department2'),
  (17,222,80,78,96,'class2','department2'),
  (18,223,79,74,96,'class2','department2'),
  (19,224,75,80,78,'class2','department2'),
  (20,225,82,85,63,'class2','department2');

-- count 开窗函数

select studentId,math,departmentId,classId,
-- 以符合条件的所有行作为窗口
count(math) over() as count1,
 -- 以按classId分组的所有行作为窗口
count(math) over(partition by classId) as count2,
 -- 以按classId分组、按math排序的所有行作为窗口
count(math) over(partition by classId order by math) as count3,
 -- 以按classId分组、按math排序、按当前行+往前1行+往后2行的行作为窗口
count(math) over(partition by classId order by math rows between 1 preceding and 2 following) as count4
from student_scores where departmentId='department1';

结果
studentid   math    departmentid    classid count1  count2  count3  count4
111         69      department1     class1  9       5       1       3
113         74      department1     class1  9       5       2       4
112         80      department1     class1  9       5       3       4
115         93      department1     class1  9       5       4       3
114         94      department1     class1  9       5       5       2
124         70      department1     class2  9       4       1       3
121         74      department1     class2  9       4       2       4
123         78      department1     class2  9       4       3       3
122         86      department1     class2  9       4       4       2

结果解释:
studentid=115,count1为所有的行数9,count2为分区class1中的行数5,count3为分区class1中math值<=93的行数4,
count4为分区class1中math值向前+1行向后+2(实际只有1)的总行数3

备注:这里不应该简单的理解order by 为排序,应该理解为窗口是到当前行,前多少行的窗口,然后是在这个窗口里面的一些聚合计算;我们可以理解sum(math) over(partition by classId order by math) as sum3中对应列的值, 理解为首先是到几行的窗口大小,然后是在窗口内的sum计算的值;

2.2 sum开窗函数

语义:求和(某一列属性的和)
同上

2.3 avg开窗函数

语义:求平均值(某一列属性的平均值)
同上
备注:因为数据到数学计算,有时为了数值美观,我们经常借助round()函数;

2.4 min开窗函数

语义:求最小值(某一列属性的最小值)
同上

2.5 max开窗函数

语义:求最大值(某一列属性的最大值)
同上

2.6 first_value开窗函数

语义:求第一个值(某一列属性的第一个值)
同上

-- first_value 开窗函数

select studentId,math,departmentId,classId,
-- 以符合条件的所有行作为窗口
first_value(math) over() as first_value1,
-- 以按classId分组的所有行作为窗口
first_value(math) over(partition by classId) as first_value2,
 -- 以按classId分组、按math排序后、按到当前行(含当前行)的所有行作为窗口
first_value(math) over(partition by classId order by math) as first_value3,
 -- 以按classId分组、按math排序后、按当前行+往前1行+往后2行的行作为窗口
first_value(math) over(partition by classId order by math rows between 1 preceding and 2 following) as first_value4
from student_scores where departmentId='department1';

结果
studentid   math    departmentid    classid first_value1    first_value2    first_value3    first_value4
111         69      department1     class1  69              69              69              69
113         74      department1     class1  69              69              69              69
112         80      department1     class1  69              69              69              74
115         93      department1     class1  69              69              69              80
114         94      department1     class1  69              69              69              93
124         70      department1     class2  69              74              70              70
121         74      department1     class2  69              74              70              70
123         78      department1     class2  69              74              70              74
122         86      department1     class2  69              74              70              78

结果解释:
    studentid=124 first_value1:第一个值是69,first_value2:classId=class1分区 math的第一个值是69

参考:https://blog.csdn.net/wangpei1949/article/details/81437574

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
HiveSQL中,开窗函数是一种特殊的函数,用于在查询结果中添加一个新的窗口函数值列。开窗函数可以分为排序开窗函数和聚合开窗函数两类。常用的排序开窗函数包括row_number()和rank()等。这些排序函数在over()子句中的order by语句只起到窗口内部排序的作用。开窗函数的基本用法包括设置窗口的方法,例如使用window_name、partition by和order by子句来指定窗口的大小和排序方式。另外,开窗函数还可以用于计算序号函数和分布函数。序号函数包括row_number()、rank()和dense_rank()等,用于计算每个行的序号。分布函数包括percent_rank()和cume_dist()等,用于计算某个值在整个结果集中的位置。此外,还有lag()和lead()函数用于获取前后指定行的值,以及first_value()和last_value()函数用于获取窗口内的第一个和最后一个值。在HiveSQL中,开窗函数可以与聚合函数结合使用,以便进行更复杂的计算和分析。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [hive sql常用开窗函数](https://blog.csdn.net/a822631129/article/details/124672228)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [hiveSQL开窗函数详解](https://blog.csdn.net/weixin_62759952/article/details/129269434)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值