求不连续的数据

liujuns 2010-07-22 11:26:06
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'



结果是:
输入 : Begin: '2006-01-01 00:00:00' END '2009-01-01 00:00:00'

SELECT 1 ,'2008-01-01 00:00:00' ,'缺'
UNION
SELECT 2,'2009-01-01 00:00:00' ,'缺'
...全文
69 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
claro 2010-07-22
  • 打赏
  • 举报
回复
看不懂

帮顶
黄_瓜 2010-07-22
  • 打赏
  • 举报
回复
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 行)


*/

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧