Hive 窗口函数之 lead() over(partition by ) 和 lag() over(partition by )

lead函数用于提取当前行前某行的数据

lag函数用于提取当前行后某行的数据

语法如下:

lead(expression,offset,default) over(partition by ... order by ...)

lag(expression,offset,default) over(partition by ... order by ... )

例如提取前一周和后一周的数据,如下:

select

  year,week,sale,

  lead(sale,1,NULL) over(--前一周sale partition by product,country,region order by year,week) lead_week_sale,

  lag(sale,1,NULL) over(--后一周sale partition by product,country,region order by year,week) lag_week_sale

from sales_fact a
where a.country='country1' and a.product='product1' and region='region1'
order by product,country,year,week

实例2:

SELECT  

  created_at create_time,

  operator,

  bridge_duration,
  lead(created_at, 1) OVER (PARTITION BY operator ORDER BY created_at ASC) next_create_time
FROM ods.ods_call_ctob_auction_call_recording
WHERE substr(created_at,1,10)= '${date_y_m_d}'


————————————————
版权声明:本文为CSDN博主「hongyd」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/hongyd/article/details/83056194

posted @ 2019-10-28 15:16  数据分析笔记(自用)  阅读(17591)  评论(2编辑  收藏  举报