【问题标题】:Find Gaps in a single date column SQL Server在单个日期列 SQL Server 中查找间隙
【发布时间】:2021-01-27 21:36:08
【问题描述】:

大家好,

我需要你的帮助。

我正在尝试检测 SQL Server 中 Date 或 DateTime 类型的单个列中的间隙。

假设我们有一个学校列表,每个学校都有很多记录,并且有一个 uploadDate 字段。

类似这样的:

我的结果会是这样的:

谢谢大家。

【问题讨论】:

  • 2020-01-04 不是也不见了吗?
  • 请不要张贴text的图片;是文字,请以text发帖。
  • 请阅读this,了解一些改进问题的技巧。

标签: sql sql-server date tsql datetime


【解决方案1】:

你可以使用lead():

select name, dateadd(day, 1, upload_date), dateadd(day, -1, next_upload_date)
from (select t.*,
             lead(upload_date) over (partition by name order by upload_date) as next_upload_date
      from t
     ) t
where next_upload_date <> dateadd(day, 1, upload_date);

【讨论】:

    猜你喜欢
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多