尝试这个
with t as(
select 'a' c,to_date('01.01.2000','mm.dd.yyyy') d from dual
union all
select 'a' ,to_date('10.05.2000','mm.dd.yyyy') from dual
union all
select 'a' ,to_date('12.02.2000','mm.dd.yyyy') from dual
union all
select 'b' ,to_date('01.01.2000','mm.dd.yyyy') from dual
union all
select 'b' ,to_date('01.31.2000','mm.dd.yyyy') from dual
union all
select 'b' ,to_date('02.01.2000','mm.dd.yyyy') from dual
union all
select 'c' ,to_date('01.01.2000','mm.dd.yyyy') from dual
)
select t.c,t.d,LAG (t.d) over ( partition by t.c order by t.d ) from t
最近的日期必须在不同的月份?好的
with t as(
select 'a' c,to_date('01.01.2000','mm.dd.yyyy') d from dual
union all
select 'a' ,to_date('10.05.2000','mm.dd.yyyy') from dual
union all
select 'a' ,to_date('12.02.2000','mm.dd.yyyy') from dual
union all
select 'b' ,to_date('01.01.2000','mm.dd.yyyy') from dual
union all
select 'b' ,to_date('01.31.2000','mm.dd.yyyy') from dual
union all
select 'b' ,to_date('02.01.2000','mm.dd.yyyy') from dual
union all
select 'c' ,to_date('01.01.2000','mm.dd.yyyy') from dual
)
select c,d,case when trunc(d,'Month')<>trunc(closest_d,'Month') then closest_d else null end closest_d from(
select t.c,t.d,LAG (t.d) over ( partition by t.c order by t.d ) closest_d from t
)