SQL Query Creating Start and End Dates

后端 未结 3 1881
无人共我
无人共我 2021-01-14 11:18

Alright, let\'s say I have a table that looks like this:

 ID   | DATE
 2  | 2010-08-12
 2  | 2010-08-16 
 2  | 2010-08-17 
 2  | 2010-12-21 
 2  | 2010-12-22         


        
3条回答
  •  醉梦人生
    2021-01-14 11:48

    I will not put here the ID as I see it is irrelevant in the query. If you wish you will put it later. This is a MSSQL query.

    select tb1.date as startdate,dateadd(d,-1,tb2.date) as enddate
    from the_table tb1
    join the_table tb2 on tb2.date>tb1.date
    left join the_table tb3 on tb1.date

    It can be easily translated for other DB types.

提交回复
热议问题