[数据]
[Emp] [Emp_group] [Date_purchase]
1 001 12-jan-2016
1 001 13-jan-2016
1 001 19-jan-2016
1 003 14-jan-2016
2 004 21-feb-2016
2 004 22-feb-2016
2 004 23-feb-2016
3 005 01-apr-2016
需要SQL查找连续的购买日期。 emp组(001)的Emp(1)已于1月12日和13日连续购买。必须考虑Emp和Emp组分区。
答案 0 :(得分:0)
只需使用lag()
/ lead()
:
select t.*
from (select t.*,
lag(date_purchase) over (partition by emp, emp_group order by date_purchase) as prev_dp,
lead(date_purchase) over (partition by emp, emp_group order by date_purchase) as next_dp
from t
) t
where date_purchase in (prev_dp, next_dp);