我在下表中列出了每个标识符的缺失日期, 我想确定错过序列的次数。
从下表中对于标识符3的含义,日期序列错过了三次。 那是来自
所以我期待的输出是
Identifier count of missed seq dates
3 3
4 4
5 1
请使用表格:
DECLARE @table TABLE (IDENTi VARCHAR(50), StartDate DATETIME)
INSERT INTO @table VALUES ('3', '12/1/2024 ')
INSERT INTO @table VALUES ('3', '12/5/2024 ')
INSERT INTO @table VALUES ('3', '12/6/2024 ')
INSERT INTO @table VALUES ('3', '12/7/2024 ')
INSERT INTO @table VALUES ('3', '12/8/2024 ')
INSERT INTO @table VALUES ('3', '12/13/2024')
INSERT INTO @table VALUES ('3', '12/14/2024')
INSERT INTO @table VALUES ('3', '12/15/2024')
INSERT INTO @table VALUES ('3', '12/16/2024')
INSERT INTO @table VALUES ('3', '12/17/2024')
INSERT INTO @table VALUES ('3', '12/18/2024')
INSERT INTO @table VALUES ('3', '12/23/2024')
INSERT INTO @table VALUES ('3', '12/24/2024')
INSERT INTO @table VALUES ('3', '12/25/2024')
INSERT INTO @table VALUES ('3', '12/26/2024')
INSERT INTO @table VALUES ('3', '12/27/2024')
INSERT INTO @table VALUES ('3', '12/28/2024')
INSERT INTO @table VALUES ('3', '12/29/2024')
INSERT INTO @table VALUES ('3', '12/30/2024')
INSERT INTO @table VALUES ('4', '12/1/2024 ')
INSERT INTO @table VALUES ('4', '12/5/2024 ')
INSERT INTO @table VALUES ('4', '12/6/2024 ')
INSERT INTO @table VALUES ('4', '12/7/2024 ')
INSERT INTO @table VALUES ('4', '12/8/2024 ')
INSERT INTO @table VALUES ('4', '12/13/2024')
INSERT INTO @table VALUES ('4', '12/14/2024')
INSERT INTO @table VALUES ('4', '12/15/2024')
INSERT INTO @table VALUES ('4', '12/16/2024')
INSERT INTO @table VALUES ('4', '12/17/2024')
INSERT INTO @table VALUES ('4', '12/18/2024')
INSERT INTO @table VALUES ('4', '12/23/2024')
INSERT INTO @table VALUES ('4', '12/24/2024')
INSERT INTO @table VALUES ('4', '12/25/2024')
INSERT INTO @table VALUES ('4', '12/26/2024')
INSERT INTO @table VALUES ('4', '12/30/2024')
INSERT INTO @table VALUES ('5', '12/23/2024')
INSERT INTO @table VALUES ('5', '12/25/2024')
答案 0 :(得分:1)
这是快速变体:
select IDENTi, count(*)
from tt as a
where
datediff(day, (select max(StartDate) from tt as b where a.IDENTi=b.IDENTi and a.StartDate>b.StartDate), a.StartDate)>1
group by IDENTi