34,588
社区成员
发帖
与我相关
我的任务
分享
DECLARE @calendar_date TABLE (Item int ,calendardate SMALLDATETIME)
INSERT INTO @calendar_date
SELECT 1 ,'2006-01-01 00:00:00'
UNION
SELECT 1, '2007-01-01 00:00:00'
UNION
SELECT 1 ,'2009-01-01 00:00:00'
UNION
SELECT 2, '2006-01-01 00:00:00'
UNION
SELECT 2, '2007-01-01 00:00:00'
UNION
SELECT 2, '2008-01-01 00:00:00'
declare @Begin datetime,@end datetime
select @Begin='2006-01-01 00:00:00', @end= '2009-01-01 00:00:00'
select Item,dateadd(yy,1,calendardate) as start,'缺'
from @calendar_date as t
where not exists
(
select 1 from @calendar_date where calendardate=dateadd(yy,1,t.calendardate)
and Item=t.Item
and t.calendardate > @Begin
and t.calendardate <@end
)
and t.calendardate > @Begin and t.calendardate <@end
/*
Item start
----------- ------------------------------------------------------ ----
1 2008-01-01 00:00:00 缺
2 2009-01-01 00:00:00 缺
(所影响的行数为 2 行)
*/