T-SQL识别日期序列中的空白

时间:2018-11-16 17:27:28

标签: sql-server tsql sequence gaps-and-islands

请帮助您解决我遇到的问题,我认为这与T-SQL中的间隙和孤岛问题有关。我正在使用SQL Server 2014。

我正在尝试使用日期列来识别断开的链之间的区别,以对表/索引组合的连续出现次数进行计数。

请参见下面的T-SQL来演示我要实现的目标,尤其是如何计算Rnk列,出于演示目的,我已对其进行了手工硬编码?

CREATE TABLE #test (RowID INT IDENTITY(1,1), FileDate DATE, TableName VARCHAR(100), IndexName VARCHAR(100), Rnk INT)

INSERT INTO #test (FileDate, TableName, IndexName, Rnk) 
VALUES
('2015-10-31', 't1', 'idx1', 1),
('2015-10-30', 't1', 'idx1', 2),

('2015-10-27', 't1', 'idx1', 1),
('2015-10-26', 't1', 'idx1', 2),
('2015-10-25', 't1', 'idx1', 3),

('2015-10-23', 't1', 'idx1', 1),
('2015-10-22', 't1', 'idx1', 2),
('2015-10-21', 't1', 'idx1', 3),
('2015-10-20', 't1', 'idx1', 4),
('2015-10-19', 't1', 'idx1', 5),
('2015-10-15', 't1', 'idx1', 1),
('2015-10-13', 't1', 'idx1', 1),
('2015-10-10', 't1', 'idx1', 1),
('2015-10-09', 't1', 'idx1', 2),

('2015-10-27', 't3', 'idx13', 1),
('2015-10-26', 't3', 'idx13', 2),
('2015-10-25', 't3', 'idx15', 1),
('2015-10-24', 't3', 'idx15', 2),
('2015-10-21', 't3', 'idx13', 1)

SELECT * FROM #test 

DROP TABLE #test

在我所附的屏幕截图中,突出显示的结果部分显示了我希望Rnk列对t1 / idx在2015-10-27-2015-10-25之间的连续出现进行排序,但请为下次出现是在2015-10-23至2015-10-19。

有人可以帮助我吗?

IMG1

谢谢。

3 个答案:

答案 0 :(得分:4)

从日期中减去一系列数字-您确定的组将具有恒定值。然后,您可以使用row_number()

select t.*,
       row_number() over (partition by tablename, indexname,
                                       dateadd(day, - seqnum, filedate)
                          order by filedate desc
                         ) as rnk
from (select t.*,
             row_number() over (partition by tablename, indexname order by filedate) as seqnum
      from t
     ) t

答案 1 :(得分:0)

我会使用累积方法:

select t.FileDate, t.TableName, t.IndexName,
       row_number() over (partition by tablename, indexname, grp order by rowid)
from (select t.*, sum(case when gap > 1 then 1 else 0 end) over (partition by tablename, indexname order by rowid) as grp
      from (select t.*, 
                   isnull(datediff(day, filedate, lag(filedate) over (partition by tablename, indexname order by rowid)), 1) as gap
            from #test t
           ) t
     ) t;

答案 2 :(得分:0)

与Yogesh的答案类似,后者击败了我。
(提示:在手机上输入答案时,不要指望更快)

SELECT 
RowID, FileDate, TableName, IndexName, 
ROW_NUMBER() OVER (PARTITION BY TableName, IndexName,  DateRank ORDER BY FileDate DESC) AS Rnk
FROM
(
  SELECT *,
  SUM(DateGap) OVER (PARTITION BY TableName, IndexName ORDER BY FileDate DESC) AS DateRank
  FROM
  (
      SELECT RowID, FileDate, TableName, IndexName,
      --  Rnk as ExpRnk,
      CASE WHEN DATEDIFF(DAY, FileDate, LAG(FileDate) OVER (PARTITION BY TableName, IndexName ORDER BY FileDate DESC)) <= 1 THEN 0 ELSE 1 END AS DateGap
      FROM #Test
  ) q1
) q2
ORDER BY RowID;