窗口函数(开窗函数)
OVER():指定分析函数工作的数据窗口大小,这个数据窗口大小可能会随着行的变而变化。
CURRENT ROW:当前行
n PRECEDING:往前n行数据
n FOLLOWING:往后n行数据
UNBOUNDED PRECEDING 起点
UNBOUNDED FOLLOWING 终点
order by[asc/desc] 有序
partition by 分组
LAG(col,n,default_val):往前第n行数据
LEAD(col,n, default_val):往后第n行数据
NTILE(n) 将数据分成n组,有序窗口
percent_rank() 显示该条记录占窗口数据的百分比
创建本地business.txt,导入数据
vi business.txt
创建hive表并导入数据
create table business(
name string,
orderdate string,
cost int
) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';
load data local inpath "/opt/module/datas/business.txt" into table business;
(1)查询在2017年4月份购买过的顾客及总人数
select name,count(*) over ()
from business
where substring(orderdate,1,7) = '2017-04'
group by name;
(2)查询顾客的购买明细及月购买总额
select name,orderdate,cost,sum(cost) over(partition by month(orderdate)) from
business;
(1)上述的场景, 将每个顾客的cost按照日期进行累加
select name,orderdate,cost,
sum(cost) over() as sample1,--所有行相加
sum(cost) over(partition by name) as sample2,--按name分组,组内数据相加
sum(cost) over(partition by name order by orderdate) as sample3,--按name分组,组内数据累加
sum(cost) over(partition by name order by orderdate rows between UNBOUNDED PRECEDING and current row ) as sample4 ,--和sample3一样,由起点到当前行的聚合
sum(cost) over(partition by name order by orderdate rows between 1 PRECEDING and current row) as sample5, --当前行和前面一行做聚合
sum(cost) over(partition by name order by orderdate rows between 1 PRECEDING AND 1 FOLLOWING ) as sample6,--当前行和前边一行及后面一行
sum(cost) over(partition by name order by orderdate rows between current row and UNBOUNDED FOLLOWING ) as sample7 --当前行及后面所有行
from business;
rows必须跟在Order by 子句之后,对排序的结果进行限制,使用固定的行数来限制分区中的数据行数量
(1)查看顾客上次的购买时间
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;
(2)查询前20%时间的订单信息
select * from (
select name,orderdate,cost, ntile(5) over(order by orderdate) sorted
from business
) t
where sorted = 1;
Rank
1)函数说明
RANK() 排序相同时会重复,总数不会变
DENSE_RANK() 排序相同时会重复,总数会减少
ROW_NUMBER() 会根据顺序计算
例题
创建hive表并导入数据
create table score(
name string,
subject string,
score int)
row format delimited fields terminated by "\t";
load data local inpath '/opt/module/datas/score.txt' into table score
(1)计算每门学科成绩排名。
select name,
subject,
score,
rank() over(partition by subject order by score desc) rp,
dense_rank() over(partition by subject order by score desc) drp,
row_number() over(partition by subject order by score desc) rmp
from score;
current_date返回当前日期
select current_date();
(2)date_add, date_sub 日期的加减
--今天开始90天以后的日期
select date_add(current_date(), 90);
--今天开始90天以前的日期
select date_sub(current_date(), 90);
(3)两个日期之间的日期差
--今天和1990年6月4日的天数差
SELECT datediff(CURRENT_DATE(), "1990-06-04");
日期函数
CURRENT_DATE() 当前日期
DATE_ADD(start_date,num_days) 返回开始日期后n天的日期
DATE_SUB(start_date,num_days) 返回开始日期前n天的日期
DATE_DIFF(date_1,date_2) 返回两个日期的差(天数)
(1)CURRENT_DATE() 当前日期
select crrent_date();
(2)DATE_ADD(start_date,num_days) 返回开始日期后n天的日期
select date_add("2020-12-30 14:55:55",1);
(3)DATE_SUB(start_date,num_days) 返回开始日期前n天的日期
select date_sub(current_date(),1);
(4)DATE_DIFF(date_1,date_2) 返回两个日期的差(天数)
selectdate地方法((date_add("2020-12-31 14:55:55",1)),(date_sub(date_sub(current_date(),1)));