1.基本语法
-
Function (arg1,..., argn) OVER ([PARTITION BY <...>] [ORDER BY <....>]
-
[<window_expression>])
Function (arg1,..., argn) 可以是下面的四类函数:
-
Aggregate Functions: 聚合函数,比如:sum(...)、 max(...)、min(...)、avg(...)等
-
Sort Functions: 数据排序函数, 比如 :rank(...)、row_number(...)等
-
Analytics Functions: 统计和比较函数, 比如:lead(...)、lag(...)、 first_value(...)等
2.数据准备
样例数据
-
[职工姓名|部门编号|职工ID|工资|岗位类型|入职时间]
-
Michael|1000|100|5000|full|2014-01-29
-
Will|1000|101|4000|full|2013-10-02
-
Wendy|1000|101|4000|part|2014-10-02
-
Steven|1000|102|6400|part|2012-11-03
-
Lucy|1000|103|5500|full|2010-01-03
-
Lily|1001|104|5000|part|2014-11-29
-
Jess|1001|105|6000|part|2014-12-02
-
Mike|1001|106|6400|part|2013-11-03
-
Wei|1002|107|7000|part|2010-04-03
-
Yun|1002|108|5500|full|2014-01-29
-
Richard|1002|109|8000|full|2013-09-01
建表语句:
-
CREATE TABLE IF NOT EXISTS employee (
-
name string,
-
dept_num int,
-
employee_id int,
-
salary int,
-
type string,
-
start_date date
-
)
-
ROW FORMAT DELIMITED
-
FIELDS TERMINATED BY '|'
-
STORED as TEXTFILE;
加载数据
-
load data local inpath '/opt/datas/data/employee_contract.txt' into table employee;
3.窗口聚合函数
(1)查询姓名、部门编号、工资以及部门人数
-
select
-
name,
-
dept_num as deptno ,
-
salary,
-
count(*) over (partition by dept_num) as cnt
-
from employee ;
结果输出:
-
name deptno salary cnt
-
Lucy 1000 5500 5
-
Steven 1000 6400 5
-
Wendy 1000 4000 5
-
Will 1000 4000 5
-
Michael 1000 5000 5
-
Mike 1001 6400 3
-
Jess 1001 6000 3
-
Lily 1001 5000 3
-
Richard 1002 8000 3
-
Yun 1002 5500 3
-
Wei 1002 7000 3
(2)查询姓名、部门编号、工资以及每个部门的总工资,部门总工资按照降序输出
-
select
-
name ,
-
dept_num as deptno,
-
salary,
-
sum(salary) over (partition by dept_num order by dept_num) as sum_dept_salary
-
from employee
-
order by sum_dept_salary desc;
结果输出:
-
name deptno salary sum_dept_salary
-
Michael 1000 5000 24900
-
Will 1000 4000 24900
-
Wendy 1000 4000 24900
-
Steven 1000 6400 24900
-
Lucy 1000 5500 24900
-
Wei 1002 7000 20500
-
Yun 1002 5500 20500
-
Richard 1002 8000 20500
-
Lily 1001 5000 17400
-
Jess 1001 6000 17400
-
Mike 1001 6400 17400
4.窗口排序函数
简介
窗口排序函数提供了数据的排序信息,比如行号和排名。在一个分组的内部将行号或者排名作为数据的一部分进行返回,最常用的排序函数主要包括:
row_number:根据具体的分组和排序,为每行数据生成一个起始值等于1的唯一序列数
rank:对组中的数据进行排名,如果名次相同,则排名也相同,但是下一个名次的排名序号会出现不连续。比如查找具体条件的topN行
dense_rank:dense_rank函数的功能与rank函数类似,dense_rank函数在生成序号时是连续的,而rank函数生成的序号有可能不连续。当出现名次相同时,则排名序号也相同。而下一个排名的序号与上一个排名序号是连续的。
(编辑:漯河站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|