sql server - Are dates consecutive -


i have simple table representing customer number , when made purchase. need know when given customer has made purchase on consecutive dates , dates were. number of consecutive dates irrelevant.

so in example

 +----------+--------+ | customer |  date  | +----------+--------+ |      123 | 21-jan | |      123 | 24-jan | |      345 | 21-jan | |      345 | 23-jan | |      345 | 24-jan | |      123 | 26-jan | |      123 | 27-jan | +----------+--------+ 

i need return following:

 +----------+--------+ | customer |  date  | +----------+--------+ |      123 | 26-jan | |      123 | 27-jan | |      345 | 23-jan | |      345 | 24-jan | +----------+--------+ 

this modified gaps , islands problem. 1 solution use row_number:

with cte as(     select *,         grp = dateadd(day, -row_number() over(partition customer order date), date)     tbl ), ctefinal as(     select *,         cnt = count(customer) over(partition customer, grp)     cte ) select     customer, date ctefinal cnt > 1 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -