hive中windows子句的使用

概述

1,windows子句是对窗口的结果做更细粒度的划分

2、windows子句中有两种方式

rows :按照相邻的几行进行开窗

range:按照某个值的范围进行开窗

使用方式

复制代码
(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

1 PRECEDING 表示前一个窗口
1 FOLLOWING 表示后一个窗口
current row 表示当前窗口
UNBOUNDED:起点,
UNBOUNDED PRECEDING:表示从前面的起点, 
UNBOUNDED FOLLOWING 表示到后面的终点 

简单使用

复制代码
姓名,购买日期,购买数量
saml,2018-01-01,10
saml,2018-01-08,55
tony,2018-01-07,50
saml,2018-01-05,46
tony,2018-01-04,29
tony,2018-01-02,15
saml,2018-02-03,23
mart,2018-04-13,94
saml,2018-04-06,42
mart,2018-04-11,75
mart,2018-04-09,68
mart,2018-04-08,62
neil,2018-05-10,12
neil,2018-06-12,80

create table sample(
    name string,
    dt string,
    num int
)
row format delimited
fields terminated by ",";
load data local inpath '/home/homedata/sample.txt' into table sample;

rows

问题:找出最近三次的购买的数量之和(也就是这次和上两次)

复制代码
select *,sum(num) over (partition by name order by dt rows between 2 PRECEDING and current row ) sum from sample ;

结果展示

range

注:1、在hive中range是不支持INTERVAL关键字的使用

2、时间字段需要转为秒值

3、值必须是计算好的 (不能是6*3600这种,需要是21600)

问题:获取每个用户最近3天购买数量

复制代码
select *,sum(num) over (partition by name order by unix_timestamp(dt,"yyyy-MM-dd") range between  259200  PRECEDING and current row ) sum from sample ;

案例

rows

复制代码
id           dt

1    2024-04-25 
1    2024-04-26 
1    2024-04-27
1    2024-04-28
1    2024-04-30
1    2024-05-01
1    2024-05-02
1    2024-05-04
1    2024-05-05
2    2024-04-25
2    2024-04-28
2    2024-05-02
2    2024-05-03
2    2024-05-04

create table sql2_20(
    id int,
    dt string
)
row format serde 'org.apache.hadoop.hive.serde2.RegexSerDe'
with serdeproperties(
  'input.regex'='(\\d+)\\s+(.+?)',
    'output.format.string'='%1$s %2$s'
);
load data local inpath '/home/homedata/sql2/sql2_20.txt' into table sql2_20;

问题:现有用户登录记录表,请查询出用户连续三天登录的所有数据记录

期望结果

答案:

复制代码
with t1 as (
    select *,date_sub(dt,row_number() over (partition by id order by dt)) p from sql2_20
), t2 as (
    select id,dt,count(*) over (partition by id,p order by dt rows between 2 PRECEDING and current row) days from t1
),t3 as (
    select id,concat(date_sub(dt,2),",",date_sub(dt,1),",",dt) dts from t2 where days = 3
)
select id,dt from t3 lateral view explode(split(dts,",")) d as dt;

range

复制代码
create table sql1_21(
    order_id int,
    user_id string,
    order_status string,
    operate_time string
)
row format serde 'org.apache.hadoop.hive.serde2.RegexSerDe'
with serdeproperties(
  'input.regex'='(\\d+)\\s+(.+?)\\s+(.+?)\\s+(.+?)'
);
load data local inpath '/home/homedata/sql_1/sql1_21.txt' into table sql1_21;
 
order_id    user_id    order_status     operate_time
1101         a         已支付        2023-01-01 10:00:00
1102         a         已取消        2023-01-01 10:10:00
1103         a         待支付        2023-01-01 10:20:00
1104         b         已取消        2023-01-01 10:30:00
1105         a         待确认        2023-01-01 10:50:00
1106         a         已取消        2023-01-01 11:00:00
1107         b         已取消        2023-01-01 11:40:00
1108         b         已取消        2023-01-01 11:50:00
1109         b         已支付        2023-01-01 12:00:00
1110         b         已取消        2023-01-01 12:11:00
1111         c         已取消        2023-01-01 12:20:00
1112         c         已取消        2023-01-01 12:30:00
1113         c         已取消        2023-01-01 12:55:00
1114         c         已取消        2023-01-01 13:00:00

问题:找出恶意购买的用户------同一个用户,在任意半小时内(含),取消订单次数>=3次的就被视为恶意买家。

结果:

复制代码
with t1 as (
    select order_id, user_id, unix_timestamp(operate_time) operate_time
    from sql1_21 where order_status = "已取消"
)
select user_id,
       count(*) over (partition by user_id order by operate_time range between 1800 preceding and current row )
from t1;
相关推荐
AI营销先锋7 小时前
2025 AI市场舆情分析行业报告:原圈科技如何帮助企业穿越迷雾,寻找增长北极星
大数据·人工智能
TDengine (老段)7 小时前
TDengine 在新能源领域的最佳实践
大数据·数据库·物联网·时序数据库·tdengine·涛思数据
Alluxio7 小时前
Alluxio正式登陆Oracle云市场,为AI工作负载提供TB级吞吐量与亚毫秒级延迟
人工智能·分布式·机器学习·缓存·ai·oracle
石像鬼₧魂石7 小时前
内网渗透靶场 攻击 & 排错命令分类速查表
linux·windows·学习·ubuntu
武子康7 小时前
Java-204 RabbitMQ Connection/Channel 工作流程:AMQP 发布消费、抓包帧结构与常见坑
java·分布式·消息队列·rabbitmq·ruby·java-activemq
郑州光合科技余经理7 小时前
海外国际版同城服务系统开发:PHP技术栈
java·大数据·开发语言·前端·人工智能·架构·php
跨境卫士苏苏7 小时前
突围新品广告泥潭:亚马逊广告底层逻辑大重构
大数据·人工智能·算法·重构·亚马逊·防关联
zhz52147 小时前
代码之恋(第十五篇:分布式心跳与网络延迟)
网络·分布式·ai·重构·vue·结对编程
深耕AI7 小时前
【CUDA安装报错?】Nsight Visual Studio Edition安装失败《终极解决方法》
windows·经验分享·gpu算力
云老大TG:@yunlaoda3608 小时前
开通华为云国际站代理商的UCS服务需要哪些资质?
大数据·数据库·华为云·云计算