Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

database - SQL Group by Date Range

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...