计算SQL中连续分组条目的数量

时间:2018-11-14 17:07:19

标签: sql sql-server tsql window-functions gaps-and-islands

我想创建并填充以下否。 SQL(SQL服务器)在Curr.Status中输入的条目数(如下所示)。

ID          Sequence    Prev.Status Curr.Status No. of Entries in Curr.Status
9-9999-9    1           Status D    Status A    1
9-9999-9    2           Status A    Status A    2
9-9999-9    3           Status A    Status A    3
9-9999-9    4           Status A    Status A    4
9-9999-9    5           Status A    Status B    1
9-9999-9    6           Status B    Status B    2
9-9999-9    7           Status B    Status B    3
9-9999-9    8           Status B    Status A    1
9-9999-9    9           Status A    Status A    2
9-9999-9    10          Status A    Status C    1
9-9999-9    11          Status C    Status C    2

是否有使用row_number()之类的快捷方法(仅此一项似乎还不够)来创建我要寻找的字段?

谢谢!

3 个答案:

答案 0 :(得分:1)

这似乎是一个“团体和岛屿”问题。但是,有很多关于如何实现此目标的示例:

WITH VTE AS(
        SELECT *
        FROM (VALUES('9-9999-9',1 ,'Status D','Status A'),
                    ('9-9999-9',2 ,'Status A','Status A'),
                    ('9-9999-9',3 ,'Status A','Status A'),
                    ('9-9999-9',4 ,'Status A','Status A'),
                    ('9-9999-9',5 ,'Status A','Status B'),
                    ('9-9999-9',6 ,'Status B','Status B'),
                    ('9-9999-9',7 ,'Status B','Status B'),
                    ('9-9999-9',8 ,'Status B','Status A'),
                    ('9-9999-9',9 ,'Status A','Status A'),
                    ('9-9999-9',10,'Status A','Status C'),
                    ('9-9999-9',11,'Status C','Status C')) V(ID, Sequence, PrevStatus,CurrStatus)),
CTE AS(            
    SELECT ID,
           [Sequence],
           PrevStatus,
           CurrStatus,
           ROW_NUMBER() OVER (PARTITION BY ID ORDER BY [Sequence]) -
           ROW_NUMBER() OVER (PARTITION BY ID,CurrStatus ORDER BY [Sequence]) AS Grp
    FROM VTE V)
SELECT ID,
       [Sequence],
       PrevStatus,
       CurrStatus,
       ROW_NUMBER() OVER (PARTITION BY Grp ORDER BY [Sequence]) AS Entries
FROM CTE;

答案 1 :(得分:0)

如果Sequence是标识列,则可以执行以下操作:

select t.*, 
       row_number() over (partition by (Sequence - seq) order by Sequence) as [No. of Entries in Curr.Status]
from (select t.*, 
             row_number() over (partition by [Curr.Status] order by Sequence) as seq
      from table t
     ) t;

否则,您需要生成两个row_numbers

select t.*, 
       row_number() over (partition by (seq1- seq2) order by Sequence) as [No. of Entries in Curr.Status]
from (select t.*, 
             row_number() over (partition by id order by Sequence) as seq1
             row_number() over (partition by id, [Curr.Status] order by Sequence) as seq2
      from table t
     ) t;

答案 2 :(得分:0)

您可以使用LAG功能标记状态更改的行,并使用SUM() OVER ()为每个组分配唯一编号。组内的编号很简单:

DECLARE @t TABLE (ID VARCHAR(100), Sequence INT, PrevStatus VARCHAR(100), CurrStatus VARCHAR(100));
INSERT INTO @t VALUES
('9-9999-9',  1, 'Status D', 'Status A'),
('9-9999-9',  2, 'Status A', 'Status A'),
('9-9999-9',  3, 'Status A', 'Status A'),
('9-9999-9',  4, 'Status A', 'Status A'),
('9-9999-9',  5, 'Status A', 'Status B'),
('9-9999-9',  6, 'Status B', 'Status B'),
('9-9999-9',  7, 'Status B', 'Status B'),
('9-9999-9',  8, 'Status B', 'Status A'),
('9-9999-9',  9, 'Status A', 'Status A'),
('9-9999-9', 10, 'Status A', 'Status C'),
('9-9999-9', 11, 'Status C', 'Status C');

WITH cte1 AS (
    SELECT *, CASE WHEN LAG(CurrStatus) OVER(ORDER BY Sequence) = CurrStatus THEN 0 ELSE 1 END AS chg
    FROM @t
), cte2 AS (
    SELECT *, SUM(chg) OVER(ORDER BY Sequence) AS grp
    FROM cte1
), cte3 AS (
    SELECT *, ROW_NUMBER() OVER(PARTITION BY grp ORDER BY Sequence) AS SeqInGroup
    FROM cte2
)
SELECT *
FROM cte3
ORDER BY Sequence

Demo on DB Fiddle