踏踏实实练SQLday1-2 lead/lag使用(股票波峰波谷)
2 lead/lag使用(股票波峰波谷)
波峰:当天的价格大于前一天和后一天
波谷:当天的价格小于前一天和后一天
select id,ds,price,case
when price>lag_price and price>lead_price then '波峰'
when price<lag_price and price<lead_price then '波谷' end as price_type
from(
select id,ds,price,lag(price,1)over(partition by.id order by ds) as lag_price,
lead(price,1)over(partition by.id order by ds) as lead_price
from test
)t