在SQL Server中按连续日期分组



我有一个包含以下列的表:

[name_of_pos] varchar,
[date_from] datetime,
[date_to] datetime

下面是我的示例数据:

name_of_pos  date_from                 date_to
----------------------------------------------------------------
Asystent     2015-08-26 08:57:49.000   2015-09-04 08:57:49.000
Biuro        2015-09-01 08:53:32.000   2015-09-01 08:53:32.000
Biuro        2015-09-02 09:00:41.000   2015-09-02 09:00:41.000
Biuro        2015-09-03 11:46:03.000   2015-09-03 11:46:03.000
Biuro        2015-09-10 09:02:11.000   2015-09-15 09:02:11.000
Koordynator  2015-09-01 09:04:06.000   2015-09-01 09:04:06.000
Projektant   2015-08-31 08:59:46.000   2015-09-01 08:59:46.000
Projektant   2015-09-02 08:00:54.000   2015-09-02 08:00:54.000
Projektant   2015-09-14 12:34:50.000   2015-09-14 12:34:50.000

我想返回的是每个name_of_pos的日期范围(date_from的min到date_to的max),但只有在日期值连续的情况下(时间部分不重要,可以在结果中忽略)。

期望的输出将是:

  name_of_pos   date_from   date_to
  ------------------------------------
  Asystent      2015-08-26  2015-09-04 
  Biuro         2015-09-01  2015-09-03 
  Biuro         2015-09-10  2015-09-15 
  Koordynator   2015-09-01  2015-09-01 
  Projektant    2015-08-31  2015-09-02 
  Projektant    2015-09-14  2015-09-14 

我尝试了一个类似于这个问题的解决方案:

如何对连续区间进行分组

但是没有运气,因为我有两个日期时间列。

这是一个使用cte迭代行(在它们被排序之后)并在分组之前检查连续天数的解决方案:

-- dummy table
CREATE TABLE #TableA
    (
      [name_of_pos] VARCHAR(11) ,
      [date_from] DATETIME ,
      [date_to] DATETIME
    );
-- insert dummy data
INSERT  INTO #TableA
        ( [name_of_pos], [date_from], [date_to] )
VALUES  ( 'Asystent', '2015-08-26 08:57:49', '2015-09-04 08:57:49' ),
        ( 'Biuro', '2015-09-01 08:53:32', '2015-09-01 08:53:32' ),
        ( 'Biuro', '2015-09-02 09:00:41', '2015-09-02 09:00:41' ),
        ( 'Biuro', '2015-09-03 11:46:03', '2015-09-03 11:46:03' ),
        ( 'Biuro', '2015-09-10 09:02:11', '2015-09-15 09:02:11' ),
        ( 'Koordynator', '2015-09-01 09:04:06', '2015-09-01 09:04:06' ),
        ( 'Projektant', '2015-08-31 08:59:46', '2015-09-01 08:59:46' ),
        ( 'Projektant', '2015-09-02 08:00:54', '2015-09-02 08:00:54' ),
        ( 'Projektant', '2015-09-14 12:34:50', '2015-09-14 12:34:50' );
-- new temp table used to add row numbers for data order
SELECT  name_of_pos, CAST(date_from AS DATE) date_from, CAST(date_to AS DATE) date_to,
        ROW_NUMBER() OVER ( ORDER BY name_of_pos, date_from ) rn
INTO    #temp
FROM    #TableA
-- GroupingColumn in cte used to identify and group consecutive dates
;WITH    cte
          AS ( SELECT   name_of_pos ,
                        date_from ,
                        date_to ,
                        1 AS GroupingColumn ,
                        rn
               FROM     #temp
               WHERE    rn = 1
               UNION ALL
               SELECT   t2.name_of_pos ,
                        t2.date_from ,
                        t2.date_to ,
                        CASE WHEN t2.date_from = DATEADD(day, 1, cte.date_to) 
                                  AND cte.name_of_pos = t2.name_of_pos
                             THEN cte.GroupingColumn
                             ELSE cte.GroupingColumn + 1
                        END AS GroupingColumn ,
                        t2.rn
               FROM     #temp t2
                        INNER JOIN cte ON t2.rn = cte.rn + 1
             )
    SELECT  name_of_pos, MIN(date_from) AS date_from, MAX(date_to) AS date_to
    FROM    cte
    GROUP BY name_of_pos, GroupingColumn
DROP TABLE #temp
DROP TABLE #TableA

产生你想要的输出:

name_of_pos date_from   date_to
Asystent    2015-08-26  2015-09-04
Biuro       2015-09-01  2015-09-03
Biuro       2015-09-10  2015-09-15
Koordynator 2015-09-01  2015-09-01
Projektant  2015-08-31  2015-09-02
Projektant  2015-09-14  2015-09-14

您可以使用cte,但根据我的经验,最快的方法是在循环中使用update:

declare @temp table
(
  name_of_pos varchar(128),
  date_from datetime,
  date_to datetime
)
insert into @temp (
    name_of_pos, date_from, date_to
) 
values
    ('Asystent', '2015-08-26 08:57:49', '2015-09-04 08:57:49'),
    ('Biuro', '2015-09-01 08:53:32', '2015-09-01 08:53:32'),
    ('Biuro', '2015-09-02 09:00:41', '2015-09-02 09:00:41'),
    ('Biuro', '2015-09-03 11:46:03', '2015-09-03 11:46:03'),
    ('Biuro', '2015-09-10 09:02:11', '2015-09-15 09:02:11'),
    ('Koordynator', '2015-09-01 09:04:06', '2015-09-01 09:04:06'),
    ('Projektant', '2015-08-31 08:59:46', '2015-09-01 08:59:46'),
    ('Projektant', '2015-09-02 08:00:54', '2015-09-02 08:00:54'),
    ('Projektant', '2015-09-14 12:34:50', '2015-09-14 12:34:50')
----------------------------------------------------------------------------------------------------
declare @temp_new table (
  name_of_pos varchar(128),
  date_from date,
  date_to date
)
insert into @temp_new (
    name_of_pos, date_from, date_to
)
select
    name_of_pos, date_from, date_to
from @temp
while @@rowcount > 0
begin
    update t1 set
        date_to = t2.date_to
    from @temp_new as t1
        inner join @temp_new as t2 on
            t2.name_of_pos = t1.name_of_pos and
            dateadd(dd, 1, t1.date_to) = t2.date_from
end
select name_of_pos, min(date_from), date_to
from @temp_new
group by name_of_pos, date_to
order by name_of_pos, date_to

这是一个缺口和岛屿问题。这是经过调整的官方方式来完成它,这将作为解决方案进行检查:

;with 
cte as (
    SELECT *,
          dateadd( day,
           - (ROW_NUMBER() OVER (
               partition by name_of_pos
               ORDER BY t.date_from
              ) +                           -- here starts tuned part --
              isnull( 
               sum( datediff(day, date_from, date_to ) ) OVER (
                partition by name_of_pos
                ORDER BY t.date_from
                ROWS BETWEEN UNBOUNDED PRECEDING and 1 PRECEDING
              ) ,0)                         -- here ends tuned part --
           ),
           date_from
          ) as Grp
    FROM t
)  
SELECT name_of_pos
    ,min(date_from) AS date_from
    ,max(date_to) AS date_to
FROM cte
GROUP BY name_of_pos, Grp
ORDER BY name_of_pos, date_from

在sqlfiddle上进行了测试(使用了一些不同的样本数据)

试一次:

SELECT name_of_pos, date_from,date_to
FROM table
ORDER BY
name_of_pos asc, date_from desc;

相关内容

  • 没有找到相关文章

最新更新