database - SQL Group by Date Range -


i have following data:

date        code 2014-08-01  2014-08-02  2014-08-03  2014-08-04  2014-08-05  2014-08-06  2014-08-07  2014-08-08  xxxx 2014-08-09  xxxx 2014-08-10  bb 2014-08-11  ccc 2014-08-12  ccc 2014-08-13  ccc 2014-08-14  ccc 2014-08-15  ccc 2014-08-16  ccc 2014-08-17  ccc 2014-08-18  xxxx 2014-08-19  xxxx 2014-08-20  xxxx 2014-08-21  xxxx 2014-08-22  xxxx 2014-08-23  xxxx 2014-08-24  xxxx 2014-08-25  xxxx 2014-08-26  xxxx 2014-08-27  xxxx 2014-08-28  xxxx 2014-08-29  xxxx 2014-08-30  xxxx 2014-08-31  xxxx 

i want group data codes date ranges output becomes:

min date    max date    code 2014-08-01  2014-08-07  2014-08-08  2014-08-09  xxxx 2014-08-10  2014-08-10  bb 2014-08-11  2014-08-17  ccc 2014-08-18  2014-08-31  xxxx 

i have thought cannot think of how group data using sql. ideas? thanks!

so, want find sequences according date same.

here trick: if take difference between row_number() on entire group , row_number() partitioned code, constant adjacent rows same code. rest aggregation:

select  min(date), max(date), code (select t.*,              (row_number() on (order date) -               row_number() on (partition code order date)              ) grpid       followingdata t      ) t group grpid, code; 

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 -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -