不复杂的删除操作我都不知道,求助
本帖最后由 z7116775 于 2012-12-07 16:02:14 编辑
表里删除TacticId相同,且TacticId之间差小于10秒内的重复数据,保留1条
例如
GUID TacticId RunDate
4E40BE74-3B43-458F-A86A-1692F197B8A6 B279AAD2-7238-4935-837D-1B4909D58366 2012-12-07 14:44:45.717
CD38694C-86EF-4597-A621-33FB25C1F432 B279AAD2-7238-4935-837D-1B4909D58361 2012-12-07 14:38:04.857
A8DABD4C-AC8D-451A-AC5C-3E538097D984 B279AAD2-7238-4935-837D-1B4909D58361 2012-12-07 14:36:42.717
4BADDF21-31E8-4741-9C09-498595AA9AA0 B279AAD2-7238-4935-837D-1B4909D58361 2012-12-07 14:37:13.217
E1FADD99-7A69-4528-8CBC-751CFDA9BC45 B279AAD2-7238-4935-837D-1B4909D58361 2012-12-07 14:36:39.873
EE0DAC2D-5013-498A-85B9-8F7005461A5A B279AAD2-7238-4935-837D-1B4909D58366 2012-12-07 15:52:09.810
4B5DD182-05FF-4EF6-A589-96F50A9C8282 B279AAD2-7238-4935-837D-1B4909D58366 2012-12-07 15:52:12.920
5BA6571A-7C69-4485-984D-CFF9A6E5C9FB B279AAD2-7238-4935-837D-1B4909D58366 2012-12-07 15:52:17.840
删除操作以后就要变成
4E40BE74-3B43-458F-A86A-1692F197B8A6 B279AAD2-7238-4935-837D-1B4909D58366 2012-12-07 14:44:45.717
CD38694C-86EF-4597-A621-33FB25C1F432 B279AAD2-7238-4935-837D-1B4909D58361 2012-12-07 14:38:04.857
4BADDF21-31E8-4741-9C09-498595AA9AA0 B279AAD2-7238-4935-837D-1B4909D58361 2012-12-07 14:37:13.217
EE0DAC2D-5013-498A-85B9-8F7005461A5A B279AAD2-7238-4935-837D-1B4909D58366 2012-12-07 15:52:09.810
------解决方案--------------------with TB as (
select GUID,TacticId,RunDate,ROW_NUMBER() over(partition by TacticId order by RunDate) as rowid
from tablename)
delete from tablename where guid in (
select b.guid
from TB as a inner join TB as b on a.rowid=b.rowid-1 and a.TacticId=b.TacticId
where DATEDIFF(S,b.RunDate,a.RunDate)<10)