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