时间天数相差5天以上并且记录条数大于2的信息

ifnotobject_id('Tempdb..#T') isnull
   
droptable #T
Go
Createtable #T([ID]int,[NAME]nvarchar(1),[DTIME]Datetime)
Insert #T
select1,N'','2007-12-15'unionall
select2,N'','2008-10-12'unionall
select3,N'','2009-10-13'unionall
select4,N'','2009-12-15'unionall
select5,N'','2009-12-17'unionall
select6,N'','2009-12-14'unionall
select7,N'','2009-12-10'unionall
select8,N'','2009-12-01'unionall
select9,N'','2009-12-10'
Go
Select*from #T

select*from[表名]wheredatediff(day,DTIme,getdate())>5and[Name]in (select[Name]from[表名]groupby[Name]havingcount([Name])>2)


declare@iint
select@i=count(*) from #T whereDateDiff(day,[DTIME],getdate()) >5
if@i>2
select*from #T whereDateDiff(day,[DTIME],getdate()) >5

/*
ID          NAME DTIME
----------- ---- -----------------------
1           张    2007-12-15 00:00:00.000
2           刘    2008-10-12 00:00:00.000
3           王    2009-10-13 00:00:00.000
7           李    2009-12-10 00:00:00.000
8           高    2009-12-01 00:00:00.000
9           金    2009-12-10 00:00:00.000

(6 行受影响)
*/

posted @ 2010-01-03 11:31  曾祥展  阅读(765)  评论(0编辑  收藏  举报