sql - Id like to group by number of days (+ or -) and use min date -


id        date          count 1,        2014-05-01    1 1,        2014-05-04    1  1,        2014-05-10    1 2,        2014-05-02    1 2,        2014-05-03    1 2,        2014-05-09    1 

if group time difference +/- 5 days, become

id        date          count 1,         2014-05-01    2 1,         2014-05-10    1 2,         2014-05-02    2 2,         2014-05-09    1 

is possible in sequel server 2012? pointers appreciated. thanks

i think want start new group when there gap of 5 days. so, if had record (1, 2014-05-07), have 1 group 1.

if so, following work:

select id, min(date), sum(count) (select t.*, sum(hasgap) on (partition id order date) grpid       (select t.*,                    (case when datediff(day,                                        lag(date) on (partition id order date),                                        date) < 5                          0 else 1                     end) hasgap             table t            ) t      ) t group id, grpid; 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

python - Django-cities exits with "killed" -

python - How to get a widget position inside it's layout in Kivy? -