简体   繁体   English

SQL连续日-Oracle

[英]SQL Consecutive Days - Oracle

[Data] [数据]

[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

Need SQL to find consecutive purchase dates. 需要SQL查找连续的购买日期。 Emp (1) of emp group (001) has purchased consecutively on 12 and 13 of January. emp组(001)的Emp(1)已于1月12日和13日连续购买。 Emp and Emp group partition must be considered. 必须考虑Emp和Emp组分区。

Just use lag() / lead() : 只需使用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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM