name,orderdate,cost
jack,
2017-01
-01,10
tony,
2017-01
-02,15
jack,
2017-02
-03,23
tony,
2017-01
-04,29
jack,
2017-01
-05,46
jack,
2017-04
-06,42
tony,
2017-01
-07,50
jack,
2017-01
-08,55
mart,
2017-04
-08,62
mart,
2017-04
-09,68
neil,
2017-05
-10,12
mart,
2017-04
-11,75
neil,
2017-06
-12,80
mart,
2017-04
-13,94
(1)查詢在2023年4月份購買過的顧客及總人數(2)查詢顧客的購買明細及月購買總額
(3)上述的場景, 將每個顧客的cost按照日期進行累加
(4)查詢每個顧客上次的購買時間
(5)查詢前20%時間的訂單資訊
select name,
count(*
)over()
from business
where substring(orderdate,0,
7)='2017-04'
group
by name;
不是要做聚合操作, 而是要在原始資料的基礎上再加一列月購買總額(按月分組)
;
name orderdate cost sum_window_0
jack 2017-01
-0110205
tony 2017-01
-0215205
tony 2017-01
-0429205
jack 2017-01
-0546205
tony 2017-01
-0750205
jack 2017-01
-0855205
jack 2017-02
-032323
mart 2017-04
-1394341
mart 2017-04
-0862341
mart 2017-04
-0968341
mart 2017-04
-1175341
jack 2017-04
-0642341
neil 2017-05
-101212
neil 2017-06
-128080
select name,
orderdate,
cost,
sum(cost)
over
(partition
bymonth
(orderdate)
)from business;
各種視窗大小
select name
, orderdate
, cost
,sum(cost)
over()
as sample1
,sum(cost)
over
(partition
by name)
as sample2
,sum(cost)
over
(partition
by name order
by orderdate)
as sample3
,sum(cost)
over
(partition
by name order
by orderdate rows
between
unbounded
preceding
andcurrent
row)
as sample4
,sum(cost)
over
(partition
by name order
by orderdate rows
between
1preceding
andcurrent
row)
as sample5
,sum(cost)
over
(partition
by name order
by orderdate rows
between
1preceding
and1
following
)as sample6
,sum(cost)
over
(partition
by name order
by orderdate rows
between
current
rowand
unbounded
following
)as sample7
from business;
select name
, orderdate
, cost
, lag(orderdate,1,
'1900-01-01'
)over
(partition
by name order
by orderdate)
as time1
, lag(orderdate,2)
over
(partition
by name order
by orderdate)
as time2
from business;
select
*from
(select name
, orderdate
, cost
, ntile(5)
over
(order
by orderdate)
as sorted
from business
) twhere sorted =
1;
Hive視窗函式練習題
0 jdbc hive2 node03 10000 create table view log name string,time string,views int row format delimited fields terminated by 0 jdbc hive2 node03 10000 ...
Hive視窗函式
1 定義 視窗函式屬於sql中比較高階的函式 mysql從8.0版本才支援視窗函式,5.6,5.7都沒有視窗函式 oracle 裡面一直支援視窗函式 hive也支援視窗函式 以下函式才是視窗函式 視窗函式 13個 lead lead col,n,default val 往後第n行資料 col 列名 ...
hive 視窗函式
hive高階函式 row number rank dense rank 這三個視窗函式的使用場景非常多 例子 員工表中,求每個崗位薪水前兩名的員工資訊 name,入職日期,sal row number over partition by job order by sal desc 這個函式會返回組內...