Hive——窗口函数(开窗函数)
Posted 今儿又长肉了
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hive——窗口函数(开窗函数)相关的知识,希望对你有一定的参考价值。
创建好文件:
vim business.txt
数据准备:
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
创建新表:
create table business(
name string,
orderdate string,
cost int
) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';
加载数据:
load data local inpath "/opt/module/data/business.txt" into table
business;
需求:查询在2017年4月份购买过的顾客及总人数。
先查询一下在2017年四月份购买过的人有哪些。
select
*
from
business
where substring(orderdate,0,7) = '2017-04';
select
name,
count(*) over()
from
business
where substring(orderdate,0,7) = '2017-04'
group by name;
需求:查询顾客的购买明细及月购买总额。
select
name,
orderdate,
cost,
sum(cost) over(partition by name,month(orderdate)) sum1
from
business;
需求:上述的场景,将每个顾客的cost按照日期进行累加。
select
name,
orderdate,
cost,
sum(cost) over(partition by name order by orderdate) sum1
from
business;
需求:查询用户上次的购买日期
select
name,
orderdate,
lag(orderdate,1) over(partition by name order by orderdate)
from
business;
需求:查询前20%时间的订单信息
select
name,
orderdate,
cost,
ntile(5) over(order by orderdate) groupID
from
business;t1
select
name,
orderdate,
cost
from
(select
name,
orderdate,
cost,
ntile(5) over(order by orderdate) groupID
from
business)t1
where groupID = 1;
以上是关于Hive——窗口函数(开窗函数)的主要内容,如果未能解决你的问题,请参考以下文章