<bdo id='N5PE4'></bdo><ul id='N5PE4'></ul>

      <legend id='N5PE4'><style id='N5PE4'><dir id='N5PE4'><q id='N5PE4'></q></dir></style></legend>
    1. <small id='N5PE4'></small><noframes id='N5PE4'>

      <i id='N5PE4'><tr id='N5PE4'><dt id='N5PE4'><q id='N5PE4'><span id='N5PE4'><b id='N5PE4'><form id='N5PE4'><ins id='N5PE4'></ins><ul id='N5PE4'></ul><sub id='N5PE4'></sub></form><legend id='N5PE4'></legend><bdo id='N5PE4'><pre id='N5PE4'><center id='N5PE4'></center></pre></bdo></b><th id='N5PE4'></th></span></q></dt></tr></i><div id='N5PE4'><tfoot id='N5PE4'></tfoot><dl id='N5PE4'><fieldset id='N5PE4'></fieldset></dl></div>
      1. <tfoot id='N5PE4'></tfoot>

        使用 SQL 检测连续的日期范围

        时间:2023-10-09

        <small id='gbWXD'></small><noframes id='gbWXD'>

        1. <legend id='gbWXD'><style id='gbWXD'><dir id='gbWXD'><q id='gbWXD'></q></dir></style></legend>

            <tfoot id='gbWXD'></tfoot>

              <bdo id='gbWXD'></bdo><ul id='gbWXD'></ul>

                <tbody id='gbWXD'></tbody>

                <i id='gbWXD'><tr id='gbWXD'><dt id='gbWXD'><q id='gbWXD'><span id='gbWXD'><b id='gbWXD'><form id='gbWXD'><ins id='gbWXD'></ins><ul id='gbWXD'></ul><sub id='gbWXD'></sub></form><legend id='gbWXD'></legend><bdo id='gbWXD'><pre id='gbWXD'><center id='gbWXD'></center></pre></bdo></b><th id='gbWXD'></th></span></q></dt></tr></i><div id='gbWXD'><tfoot id='gbWXD'></tfoot><dl id='gbWXD'><fieldset id='gbWXD'></fieldset></dl></div>

                  本文介绍了使用 SQL 检测连续的日期范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想填充需要开始和结束日期信息的日历对象.我有一个包含日期序列的列.有些日期是连续的(有一天的差异),有些则不是.

                  I want to fill the calendar object which requires start and end date information. I have one column which contains a sequence of dates. Some of the dates are consecutive (have one day difference) and some are not.

                  InfoDate  
                  
                  2013-12-04  consecutive date [StartDate]
                  2013-12-05  consecutive date
                  2013-12-06  consecutive date [EndDate]
                  
                  2013-12-09                   [startDate]
                  2013-12-10                   [EndDate]
                  
                  2014-01-01                   [startDate]
                  2014-01-02 
                  2014-01-03                   [EndDate]
                  
                  2014-01-06                   [startDate]
                  2014-01-07                   [EndDate]
                  
                  2014-01-29                   [startDate]
                  2014-01-30 
                  2014-01-31                   [EndDate]
                  
                  2014-02-03                   [startDate]
                  2014-02-04                   [EndDate]
                  

                  我想选择每个连续日期范围的开始和结束日期(块中的第一个和最后一个).

                  I want to pick each consecutive dates range’s start and end date (the first one and the last one in the block).

                  StartDate     EndDate
                  
                  2013-12-04    2013-12-06
                  2013-12-09    2013-12-10
                  2014-01-01    2014-01-03
                  2014-01-06    2014-01-07
                  2014-01-29    2014-01-31
                  2014-02-03    2014-02-04
                  

                  我只想用 SQL 解决问题.

                  I want to solve the problem using SQL only.

                  推荐答案

                  不需要连接或递归 CTE.标准的间隙和岛解决方案是按(值减去 row_number)分组,因为它在连续序列中是不变的.开始和结束日期只是组的 MIN() 和 MAX().

                  No joins or recursive CTEs needed. The standard gaps-and-island solution is to group by (value minus row_number), since that is invariant within a consecutive sequence. The start and end dates are just the MIN() and MAX() of the group.

                  WITH t AS (
                    SELECT InfoDate d,ROW_NUMBER() OVER(ORDER BY InfoDate) i
                    FROM @d
                    GROUP BY InfoDate
                  )
                  SELECT MIN(d),MAX(d)
                  FROM t
                  GROUP BY DATEDIFF(day,i,d)
                  

                  这篇关于使用 SQL 检测连续的日期范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在多列中查找重复项? 下一篇:sql仅按顺序分组

                  相关文章

                  1. <tfoot id='y6IX8'></tfoot>

                      <legend id='y6IX8'><style id='y6IX8'><dir id='y6IX8'><q id='y6IX8'></q></dir></style></legend>
                        <bdo id='y6IX8'></bdo><ul id='y6IX8'></ul>

                      <small id='y6IX8'></small><noframes id='y6IX8'>

                      <i id='y6IX8'><tr id='y6IX8'><dt id='y6IX8'><q id='y6IX8'><span id='y6IX8'><b id='y6IX8'><form id='y6IX8'><ins id='y6IX8'></ins><ul id='y6IX8'></ul><sub id='y6IX8'></sub></form><legend id='y6IX8'></legend><bdo id='y6IX8'><pre id='y6IX8'><center id='y6IX8'></center></pre></bdo></b><th id='y6IX8'></th></span></q></dt></tr></i><div id='y6IX8'><tfoot id='y6IX8'></tfoot><dl id='y6IX8'><fieldset id='y6IX8'></fieldset></dl></div>