HIVE場景題之窗口函數二

1.1.5你知道的排名函數有哪些?說一說它們之間的區別?
排名函數:
row_number() over() : 排名函數,沒有並列名次,名次連續,如:1,2,3. 適合於生成主鍵或者不併列排名,
rank() over() : 排名函數,有並列名次,名次不連續。如:1,1,3. dense_rank() over() : 排名函數,有並列名次,名次連續。如:1,1,2.

區別:
1、(共同點)三者都可以用於分組排名,名次都是遞增。
2、(不同點)row_number()沒有並列名次,名次連續,如:1,2,3; rank()有並列名次,名次不連續, 如:1,1,3。dense_rank()有並列名次,名次連續。如:1,1,2。
1.6 score-nvl 列做減法
1.1.6編寫sql語句實現每班前三名,分數一樣並列,同時求出前三名按名次排序的一次的分差:
數據:
stu表
Stu_no class score
1 1901 90
2 1901 90
3 1901 83
4 1901 60
5 1902 66
6 1902 23
7 1902 99
8 1902 67
9 1902 87
結果數據:
班級 stu_no score rn rn1 rn_diff
1901 1 90 1 1 90
1901 2 90 2 1 0
1901 3 83 3 1 -7
1902 7 99 1 1 99
1902 9 87 2 2 -12
1902 8 67 3 3 -20

select *,
score-nvl(lag(score)over(partition by class order by l2),0) l3
from
(
select * from
(
select *,
dense_rank()over(partition by class order by score desc) l1,
row_number()over(partition by class order by score desc) l2 from stu
) b where b.l2 < 4
) c;

1.1.7每個店鋪的當月銷售額和累計到當月的總銷售額
數據:
店鋪,月份,金額
a,01,150
a,01,200
b,01,1000
b,01,800
c,01,250
c,01,220
b,01,6000
a,02,2000
a,02,3000
b,02,1000
b,02,1500
c,02,350
c,02,280
a,03,350
a,03,250

編寫Hive的HQL語句求出每個店鋪的當月銷售額和累計到當月的總銷售額?

參考答案
看sale表
select t.id,t.month,t.sum,
max(t.sum) over(partition by t.id order by t.month) max,
sum(t.sum) over(partition by t.id order by t.month) summ
from
(select id,month,sum(money) sum from sale group by id,month) t

1.1.9訂單及訂單類型行列互換
t1表:
order_id order_type order_time
111 N 10:00
111 A 10:05
111 B 10:10
是用hql獲取結果如下:
order_id order_type_1 order_type_2 order_time_1 order_time_2
111 N A 10:00 10:05
111 A B 10:05 10:10

參考答案
select * from order_type where type<>‘B’ union all
select * from order_type where type<>‘N’

窗口函數 就是你想顯示誰 就顯示誰 可以獨自開一列進行顯示 而是還有特性
select * from(
select
id,
type,
lead(type) over (partition by id order by time) a,
time,
lead(time) over (partition by id order by time) b
from order_table) c where c.type<>‘B’

1.1.10某APP每天訪問數據存放在表access_log裏面,包含日期字段ds,用戶類型字段user_type,用戶賬號user_id,用戶訪問時間log_time,請使用hive的hql語句實現如下需求:
(1)、每天整體的訪問UV、PV?
(2)、每天每個類型的訪問UV、PV?
(3)、每天每個類型中最早訪問時間和最晚訪問時間?
(4)、每天每個類型中訪問次數最高的10個用戶?
(1)、每天整體的訪問UV、PV?
select
count(1) as pv, count(distinct user_id) as uv from access_log
group by ds
;
(2)、每天每個類型的訪問UV、PV? select
user_type, count(1) as pv,
count(distinct user_id) as uv from access_log
group by user_type,ds
;
(3)、每天每個類型中最早訪問時間和最晚訪問時間? select
first_value(log_time) over(distribute by user_type,ds sort by log_time), last_value(log_time) over(distribute by user_type,ds sort by log_time) from access_log
;

(4)、每天每個類型中訪問次數最高的10個用戶?
select * from (select user_type, ds,
row_number() over(distribute by user_type,ds sort by log_time) rm from access_log) tmp
where rm < 10
;

1.1.11每個用戶連續登陸的最大天數?
數 據 : login表uid,date
1,2019-08-01
1,2019-08-02
1,2019-08-03
2,2019-08-01
2,2019-08-02
3,2019-08-01
3,2019-08-03
4,2019-07-28
4,2019-07-29
4,2019-08-01
4,2019-08-02
4,2019-08-03

結果如下: uid cnt_days 1 3
2 2
3 1
4 3

create table lg_cnt( uid int,
dt string
)
row format delimited fields terminated by ‘,’
;
load data local inpath ‘/hivedata/lg_cnt.txt’ overwrite into table lg_cnt;

select uid, max(cn) from (select uid,
count(*) cn from (select
uid,
date_sub(dt,row_number() over(distribute by uid sort by dt)) dt
from lg_cnt) t1
group by uid,dt) t2 group by uid

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章