我有以下
id eventid startdate enddate
1 1 2009-01-03 2009-01-05
1 2 2009-01-05 2009-01-09
1 3 2009-01-12 2009-01-15
如何生成与每个 eventid 相关的缺失日期?
编辑: 将根据 eventid 找出缺失的差距。例如,对于 eventid 1,输出应该是 1/3/2009,1/4/2009,1/5/2009.. 对于 eventtype id 2,它将是 1/5/2009, 1/6/2009... 到 1 /9/2009 等
我的任务是找出两个给定日期之间缺少的日期。
这是我到目前为止所做的全部事情
declare @tblRegistration table(id int primary key,startdate date,enddate date)
insert into @tblRegistration
select 1,'1/1/2009','1/15/2009'
declare @tblEvent table(id int,eventid int primary key,startdate date,enddate date)
insert into @tblEvent
select 1,1,'1/3/2009','1/5/2009' union all
select 1,2,'1/5/2009','1/9/2009' union all
select 1,3,'1/12/2009','1/15/2009'
;with generateCalender_cte as
(
select cast((select startdate from @tblRegistration where id = 1 )as datetime) DateValue
union all
select DateValue + 1
from generateCalender_cte
where DateValue + 1 <= (select enddate from @tblRegistration where id = 1)
)
select DateValue as missingdates from generateCalender_cte
where DateValue not between '1/3/2009' and '1/5/2009'
and DateValue not between '1/5/2009' and '1/9/2009'
and DateValue not between '1/12/2009'and'1/15/2009'
实际上我想要做的是,我已经生成了一个日历表,并从那里我试图根据 id 找出丢失的日期
理想的输出将是
eventid missingdates
1 2009-01-01 00:00:00.000
1 2009-01-02 00:00:00.000
3 2009-01-10 00:00:00.000
3 2009-01-11 00:00:00.000
而且它必须是基于 SET 的,并且开始和结束日期不应该是硬编码的
提前感谢