SQL Group by Date Range

半城伤御伤魂 提交于 2019-12-31 01:58:12

问题


I have the following data:

Date        Code
2014-08-01  A
2014-08-02  A
2014-08-03  A
2014-08-04  A
2014-08-05  A
2014-08-06  A
2014-08-07  A
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 to group the data with codes but also with date ranges so that the output becomes:

Min Date    Max Date    Code
2014-08-01  2014-08-07  A
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 about it but cannot think of how to group this data using SQL. Any ideas? Thanks!


回答1:


So, you want to find sequences according to the date that are the same.

Here is a trick: if you take the difference between row_number() over the entire group and row_number() partitioned by code, then it will be constant for adjacent rows with the same code. The rest is just aggregation:

select  min(date), max(date), code
from (select t.*,
             (row_number() over (order by date) -
              row_number() over (partition by code order by date)
             ) as grpid
      from followingdata t
     ) t
group by grpid, code;


来源:https://stackoverflow.com/questions/25364654/sql-group-by-date-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!