我有一个具有以下结构的表:
TABLE [dbo].[UploadedFiles](
[ID] [int] IDENTITY(1,1) NOT NULL,
[FileName] [varchar](100) NULL,
[FilePath] [varchar](200) NULL,
[UploadedBy] [varchar](50) NULL,
[DateCreated] [datetime] NULL,
我需要根据[DateCreated]列找到并显示缺少的日期。例如,如果本月3日没有数据存入数据库的[DateCreated]列,那么我将找到它并显示。但不幸的是,我无法找到办法做到这一点。如果有人在这里可以帮助我,我将非常感激。 提前谢谢。
答案 0 :(得分:0)
Tally / Calendar表是可行的方法。
但是,您可以通过临时计数表
来完成此操作示例强>
Select MissingDate = D
From (Select Top (DateDiff(DAY,(Select min(DateCreated) from UploadedFiles),(Select max(DateCreated) from UploadedFiles))+1)
D=DateAdd(DAY,-1+Row_Number() Over (Order By (Select Null)),cast((Select min(DateCreated) from UploadedFiles) as date))
From master..spt_values n1,master..spt_values n2) A
Left Join UploadedFiles B on D=cast(DateCreated as date)
Where DateCreated is null
<强>返回强>
MissingDate
2017-06-03
示例数据
DateCreated
2017-06-01 11:47:00.000
2017-06-02 11:41:00.000
2017-06-04 11:55:00.000
2017-06-05 11:79:00.000