添加行以使用SQL Server生成摘要

时间:2016-12-05 00:15:49

标签: sql-server

鉴于以下有序数据集(间隔1分钟),我如何使用Microsoft SQL Server创建一个新记录集,该记录集将给定项目的所有单个代码相加,并显示如下所示的摘要?我想我应该可以在分区上使用某种聚合函数,但我不知道从哪里开始。

请注意,DateTimedatetime,其余列的类型为varchar。我正在使用SQL Server 2012 Express。

输入数据:

 DateTime              Item      Code    Description
 2016-12-02 16:34:00   0         1       Some Description
 2016-12-02 16:35:00   0         1       Some Description
 2016-12-02 16:36:00   0         1       Some Description
 2016-12-02 16:37:00   0         1       Some Description
 2016-12-02 16:38:00   0         1       Some Description
 2016-12-02 16:39:00   0         1       Some Description
 2016-12-02 16:40:00   0         2       Some Description
 2016-12-02 16:41:00   0         2       Some Description
 2016-12-02 16:42:00   0         2       Some Description
 2016-12-02 16:43:00   0         4       Some Description
 2016-12-02 16:44:00   0         4       Some Description
 2016-12-02 16:45:00   0         4       Some Description
 2016-12-02 16:46:00   0         4       Some Description
 2016-12-02 16:47:00   0         4       Some Description
 2016-12-02 16:48:00   1         1       Some Description
 2016-12-02 16:49:00   1         2       Some Description
 2016-12-02 16:50:00   1         2       Some Description
 2016-12-02 16:51:00   1         2       Some Description
 2016-12-02 16:52:00   1         2       Some Description
 2016-12-02 16:53:00   1         3       Some Description
 2016-12-02 16:54:00   0         1       Some Description
 2016-12-02 16:55:00   0         1       Some Description
 2016-12-02 16:56:00   0         1       Some Description
 2016-12-02 16:57:00   0         8       Some Description
 2016-12-02 16:58:00   0         8       Some Description
 2016-12-02 16:59:00   0         8       Some Description
 2016-12-02 17:00:00   0         6       Some Description
 2016-12-02 17:01:00   0         6       Some Description

预期的输出数据(开始日期应该是第一次出现代码的日期时间,结束日期应该是代码的最后一次出现):

 Start DT             End DT               Item      Code    Description
 2016-12-02 16:34:00  2016-12-02 16:39:00  0         1       Some Description
 2016-12-02 16:40:00  2016-12-02 16:42:00  0         2       Some Description
 2016-12-02 16:43:00  2016-12-02 16:47:00  0         4       Some Description
 2016-12-02 16:48:00  2016-12-02 16:49:00  1         1       Some Description
 2016-12-02 16:50:00  2016-12-02 16:52:00  1         2       Some Description
 2016-12-02 16:53:00  2016-12-02 16:53:00  1         3       Some Description
 2016-12-02 16:54:00  2016-12-02 16:56:00  0         1       Some Description
 2016-12-02 16:57:00  2016-12-02 16:59:00  0         8       Some Description
 2016-12-02 17:00:00  2016-12-02 17:01:00  0         6       Some Description

1 个答案:

答案 0 :(得分:2)

你想要的是对连续日期的岛屿进行分组。 Here是Jeff Moden撰写的一篇文章,提供了解决此问题的方法:

WITH Cte AS(
    SELECT *,
        rn = DATEADD(MINUTE, - ROW_NUMBER() OVER(PARTITION BY Item, Code ORDER BY DateTime), Datetime)
    FROM Data
)
SELECT
    StartDate   = MIN(DateTime),
    EndDate     = MAX(DateTime),
    Item,
    Code,
    Description = MAX(Description)
FROM Cte
GROUP BY 
    Item, Code, rn
ORDER BY
    StartDate, EndDate;

ONLINE DEMO