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

相關文章
相關標籤/搜索