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

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

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -