日期:2014-05-16 浏览次数:20639 次
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2014-03-01 13:02:45
-- Verstion:
-- Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Enterprise Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([ID] int,[ROWID] varchar(3),[datetime] datetime)
insert [tb]
select 2,'001','2013-06-01 09:12:31' union all
select 119,'001','2013-06-01 09:12:31' union all
select 526,'001','2013-05-21 11:33:17' union all
select 3,'002','2013-07-01 09:12:31' union all
select 120,'002','2013-07-01 09:12:31' union all
select 4,'002','2013-07-05 09:12:00' union all
select 5,'003','2013-08-01 09:12:31' union all
select 6,'003','2013-08-01 09:12:31' union all
select 7,'003','2013-07-05 09:12:00'
--------------开始查询--------------------------
SELECT * FROM tb t WHERE NOT EXISTS(SELECT 1 FROM tb WHERE ROWID=t.ROWID AND ([datetime]>t.[datetime] OR [datetime]=t.[datetime] AND id>t.id))
----------------结果----------------------------
/* ID ROWID datetime
----------- ----- -----------------------
119 001 2013-06-01 09:12:31.000
4 002 2013-07-05 09:12:00.000
6 003 2013-08-01 09:12:31.000
(3 行受影响)
*/