2

我有以下代码

declare @test table (id int, [Status] int, [Date] date)

insert into @test (Id,[Status],[Date]) VALUES
    (1,1,'2018-01-01'),
    (2,1,'2018-01-01'),
    (1,1,'2017-11-01'),
    (1,2,'2017-10-01'),
    (1,1,'2017-09-01'),
    (2,2,'2017-01-01'),
    (1,1,'2017-08-01'),
    (1,1,'2017-07-01'),
    (1,1,'2017-06-01'),
    (1,2,'2017-05-01'),
    (1,1,'2017-04-01'),
    (1,1,'2017-03-01'),
    (1,1,'2017-01-01')

SELECT
    id,
    [Status],
MIN([Date]) OVER (PARTITION BY id,[Status] ORDER BY [Date],id,[Status] ) as WindowStart,
max([Date]) OVER (PARTITION BY id,[Status] ORDER BY [Date],id,[Status]) as WindowEnd,
COUNT(*) OVER (PARTITION BY id,[Status] ORDER BY [Date],id,[Status] ) as total
from @test

但结果是这样的:

id  Status  WindowStart WindowEnd   total
1   1   2017-01-01  2017-01-01  1
1   1   2017-01-01  2017-03-01  2
1   1   2017-01-01  2017-04-01  3
1   1   2017-01-01  2017-06-01  4
1   1   2017-01-01  2017-07-01  5
1   1   2017-01-01  2017-08-01  6
1   1   2017-01-01  2017-09-01  7
1   1   2017-01-01  2017-11-01  8
1   1   2017-01-01  2018-01-01  9
1   2   2017-05-01  2017-05-01  1
1   2   2017-05-01  2017-10-01  2
2   1   2018-01-01  2018-01-01  1
2   2   2017-01-01  2017-01-01  1

我需要像这样按窗口分组。

id  Status  WindowStart WindowEnd   total
1   1   2017-01-01  2017-04-01  3
1   2   2017-05-01  2017-05-01  1
1   1   2017-06-01  2017-09-01  4
1   2   2017-10-01  2017-10-01  1
1   1   2017-11-01  2018-01-01  2
2   1   2018-01-01  2018-01-01  1
2   2   2017-01-01  2017-01-01  1

id= 1 Status = 1 的第一组应该以 Status = 2 (2017-05-01) 的第一行结束,因此总数为 3,然后从 2017-06-01 重新开始到 2017-09-01共有4行。

怎样才能做到这一点?

4

2 回答 2

1

这是一个“经典”的团体和岛屿问题。互联网上可能有 1000 多个答案。

这适用于您所追求的,但是,请尝试在手头进行更多的研究。:)

WITH Groups AS(
    SELECT t.*,
           ROW_NUMBER() OVER (PARTITION BY id ORDER BY [Date]) - 
           ROW_NUMBER() OVER (PARTITION BY id, [status] ORDER BY [Date]) AS Grp
    FROM @test t)
SELECT G.id,
       G.[Status],
       MIN([Date]) AS WindowStart,
       MAX([date]) AS WindowsEnd,
       COUNT(*) AS Total
FROM Groups G
GROUP BY G.id,
         G.[Status],
         G.Grp
ORDER BY G.id, WindowStart;

请注意,在此解决方案中,最后两行的顺序是相反的;看来您在预期结果中ASCENDING为 id 1 和2 订购。DESCENDING

于 2018-01-18T15:55:16.527 回答
0

这是使用LAG函数的一种方法

;WITH cte
     AS (SELECT *,
                grp = Sum(CASE WHEN prev_val = Status THEN 0 ELSE 1 END)
                        OVER(partition BY id ORDER BY Date)
         FROM   (SELECT *,
                        prev_val = Lag(Status)OVER(partition BY id ORDER BY Date)
                 FROM   @test) a)
SELECT id,
       Status,
       WindowStart = Min(date),
       WindowEnd = Max(date),
       Total = Count(*)
FROM   cte
GROUP  BY id, Status, grp 

使用lag函数首先查找每个日期的先前状态,然后Sum over()仅在状态发生变化时通过增加数字来创建组。

于 2018-01-18T15:55:48.820 回答