PLSQL中lag和lead取前一条/后一条以及分组取最大/最小

select
to_date(period,'yyyymmdd') as ddate,
to_number(substr(period,1,4)) as iyear,
to_number(substr(period,5,2)) as imonth,
to_number(substr(period,7,2)) as iday,
period,prod_chn,prod_eng,ds_chn,cata_name,cata_name_1,kof_chn,mark_chn,pric_type_chn,pric_term_chn,item_type,unit_chn,freq
,lag(aver_pric) over (PARTITION BY prod_chn,mark_chn ORDER BY to_number(period)) as PreviousValue
,lead(aver_pric) over (PARTITION BY prod_chn,mark_chn ORDER BY to_number(period)) as NextValue
,max(aver_pric) over (partition by prod_chn,mark_chn) as MaxValue
,min(aver_pric) over (partition by prod_chn,mark_chn) as MinValue
,aver_pric
from main.tbmb_ref_market_chm
where to_number(substr(period,1,4))=to_number(to_char(sysdate,'yyyy'))
原文地址:https://www.cnblogs.com/sshh/p/1271168.html