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

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

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

语法以下:.net

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

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

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

selectclass

  year,week,sale,date

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

  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

相关文章
相关标签/搜索