Hive官网,点我就进 oracle,sqlserver都提供了窗口函数,但是在mysql5.5和5.6都没有提供窗口函数!
窗口函数: 窗口+函数
窗口: 函数运行时计算的数据集的范围函数: 运行的函数! 仅仅支持以下函数:排名分析:
RANKROW_NUMBERDENSE_RANKCUME_DISTPERCENT_RANKNTILE注意:不是所有的函数在运行都是可以通过改变窗口的大小,来控制计算的数据集的范围!所有的排名函数和LAG,LEAD,支持使用over(),但是在over()中不能定义 window_clause
格式: 函数 over( partition by 字段 ,order by 字段 window_clause )
(9) 查询前20%时间的订单信息 精确算法:
select * from (select name,orderdate,cost,cume_dist() over(order by orderdate ) cdnum from business) tmp where cdnum<=0.2不精确计算:
select * from (select name,orderdate,cost,ntile(5) over(order by orderdate ) cdnum from business) tmp where cdnum=1(8)查询顾客的购买明细及顾客最近三次cost花费
最近三次: 当前和之前两次 或 当前+前一次+后一次
当前和之前两次:
select name,orderdate,cost,sum(cost) over(partition by name order by orderdate rows between 2 PRECEDING and CURRENT row) from business当前+前一次+后一次:
select name,orderdate,cost,sum(cost) over(partition by name order by orderdate rows between 1 PRECEDING and 1 FOLLOWING) from business或
select name,orderdate,cost,cost+ lag(cost,1,0) over(partition by name order by orderdate )+ lead(cost,1,0) over(partition by name order by orderdate ) from business(7) 查询顾客的购买明细及顾客本月最后一次购买的时间
select name,orderdate,cost,LAST_VALUE(orderdate,true) over(partition by name,substring(orderdate,1,7) order by orderdate rows between CURRENT row and UNBOUNDED FOLLOWING) from business(6) 查询顾客的购买明细及顾客本月第一次购买的时间
select name,orderdate,cost,FIRST_VALUE(orderdate,true) over(partition by name,substring(orderdate,1,7) order by orderdate ) from business(5) 查询顾客的购买明细及顾客下次的购买时间
select name,orderdate,cost,lead(orderdate,1,'无数据') over(partition by name order by orderdate ) from business(4)查询顾客的购买明细及顾客上次的购买时间
select name,orderdate,cost,lag(orderdate,1,'无数据') over(partition by name order by orderdate ) from business(3)查询顾客的购买明细要将cost按照日期进行累加
select name,orderdate,cost,sum(cost) over(partition by name order by orderdate ) from business(2)查询顾客的购买明细及月购买总额
select name,orderdate,cost,sum(cost) over(partition by name,substring(orderdate,1,7) ) from business(1)查询在2017年4月份购买过的顾客及总人数
select name,count(*) over(rows between UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING) from business where substring(orderdate,1,7)='2017-04' group by name等价于
select name,count(*) over() from business where substring(orderdate,1,7)='2017-04' group by name