Hive窗口函数及练习

参考文章:Hive窗口函数详解—及3套案例练习(内容更详细)
1.窗口函数over()的语法结构

分析函数 over (partition by 列名 order by 列名 rows between 开始位置 and 结束位置)

over()函数包括三个函数:
分区 partition by 列名
排序 order by 列名
指定窗口范围 rows between 开始位置 and 结束位置

ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW(表示从起点到当前行)
ROWS BETWEEN 2 PRECEDING AND 1 FOLLOWING(表示往前2行到往后1行)
ROWS BETWEEN 2 PRECEDING AND 1 CURRENT ROW(表示往前2行到当前行)
ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING(表示当前行到终点)

这三个函数可以同时使用也可以不使用

over()函数中如果不使用这三个函数,窗口大小是针对查询产生的所有数据,如果指定了分区,窗口大小是针对每个分区的数据。

2.常与over()一起使用的分析函数
(1)聚合类
avg()、sum()、max()、min()

(2)排名类
row_number()、rank() 、dense_rank()

(3)其他类
lag(列名,往前的行数,[行数为null时的默认值,不指定为null]),可以计算用户上次购买时间,或者用户下次购买时间。
lead(列名,往后的行数,[行数为null时的默认值,不指定为null])
ntile(n) 把有序分区中的行分发到指定数据的组中,各个组有编号,编号从1开始,对于每一行,ntile返回此行所属的组的编号

针对一份电商客户订单明细表(orderinfo)进行分析

#1.统计支付订单总数
SELECT * , count(orderId) over() as total  from data.orderinfo
where isPaid = 1;
#最后生成的订单总数会填充成一个新的列
SELECT count(orderId) as total from data.orderinfo
where isPaid = 1;
#最后生成的一个临时表记录订单总数


#2.统计每天支付订单的总数
select *,count(*) over (partition by date_format(paidTime,'%Y-%m-%d')) as daytotal  from data.orderinfo
where isPaid =1 and paidTime <> 0;

#3计算从第一天到当前行的所有price大于4000的订单总数
select*,count(*) over (order by paidTime  rows between unbounded preceding and current row) as total from data.orderinfo
where isPaid =1 
and paidTime <> 0
and price >4000;


#4.查询在2016年4月份购买过的顾客信息及总订单数
select *,count(orderId) over () as total from data.orderinfo
where isPaid = 1
and substr(date_format(paidTime,'%Y-%m-%d'),1,7) = '2016-04'
order by userId;


#5.查询每个顾客的购买明细及月购买总金额
select *,round(sum(price) over (partition by userId,substr(paidTime,1,7)),2) as total_amount from data.orderinfo
where isPaid = 1
and paidTime <> 0;


#6.查询每个顾客的购买明细及到目前为止每个顾客购买总金额
select *,round(sum(price) over (partition by userId order by paidTime rows between unbounded preceding and current row)) 累计金额 from data.orderinfo
where isPaid = 1
and paidTime <> 0;


#7查询顾客上次的购买时间
select *,lag(paidTime,1) over (partition by userId order by paidTime) last_time from data.orderinfo
where isPaid = 1
and paidTime <> 0;


#8查询支付金额前20%的订单信息
select*
from(select *,ntile(5) over (order by price desc) group_num from data.orderinfo
     where isPaid = 1
     and paidTime <> 0) t
where group_num = 1;

针对员工工资信息表(emp)分析

#每个部门员工工资由高到低进行排序(三种排名方法)
SELECT * ,
SAL+ifnull(COMM,0) AS TSAL ,
row_number() over (partition by DEPTNO order by SAL+ifnull(COMM,0) desc) r1,
rank() over (partition by DEPTNO order by SAL+ifnull(COMM,0) desc) r2,
dense_rank() over (partition by DEPTNO order by SAL+ifnull(COMM,0) desc) r3
FROM bjpowernode.emp;

#每个部门工资前三的员工
select *
from (select *,
      SAL+ifnull(COMM,0) AS TSAL,
	  rank() over (partition by DEPTNO order by SAL+ifnull(COMM,0) desc) r
	  from bjpowernode.emp) t
where r <= 3;

你可能感兴趣的:(数据库)