@mysql
Hive官網,點我就進
oracle,sqlserver都提供了窗口函數,可是在mysql5.5和5.6都沒有提供窗口函數!算法
窗口函數: 窗口+函數sql
排名分析:express
注意:不是全部的函數在運行都是能夠經過改變窗口的大小,來控制計算的數據集的範圍!全部的排名函數和LAG,LEAD,支持使用over(),可是在over()中不能定義 window_clauseapache
格式: 函數 over( partition by 字段 ,order by 字段 window_clause )windows
(rows | range) between (unbounded | [num]) preceding and ([num] preceding | current row | (unbounded | [num]) following) (rows | range) between current row and (current row | (unbounded | [num]) following) (rows | range) between [num] following and (unbounded | [num]) following
(9) 查詢前20%時間的訂單信息
精確算法:oracle
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花費sqlserver
最近三次: 當前和以前兩次 或 當前+前一次+後一次scala
當前和以前兩次:
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