|
发表于 2011-10-29 11:01:16
|
显示全部楼层
SQL code --> --> (Roy)生成測試數據
declare @T table([ID] int,[SN] int,[RecordTime] Datetime)
Insert @T
select 100000018,31,'2011-10-28 16:52:00.307' union all
select 100000009,31,'2011-10-28 16:51:51.193' union all
select 100000000,31,'2011-10-28 16:51:42.267' union all
select 100000019,32,'2011-10-28 16:52:01.327' union all
select 100000010,32,'2011-10-28 16:51:52.207' union all
select 100000001,32,'2011-10-28 16:51:43.070' union all
select 100000020,33,'2011-10-28 16:52:02.337' union all
select 100000011,33,'2011-10-28 16:51:53.220' union all
select 100000002,33,'2011-10-28 16:51:44.090' union all
select 100000021,34,'2011-10-28 16:52:03.360' union all
select 100000012,34,'2011-10-28 16:51:54.233' union all
select 100000003,34,'2011-10-28 16:51:45.107' union all
select 100000022,35,'2011-10-28 16:52:04.370' union all
select 100000013,35,'2011-10-28 16:51:55.240' union all
select 100000004,35,'2011-10-28 16:51:46.123' union all
select 100000014,36,'2011-10-28 16:51:56.250' union all
select 100000005,36,'2011-10-28 16:51:47.137'
Select * from @T as a where [RecordTime]=(select MAX([RecordTime]) from @T where SN=a.SN)
/*
ID SN RecordTime
100000018 31 2011-10-28 16:52:00.307
100000019 32 2011-10-28 16:52:01.327
100000020 33 2011-10-28 16:52:02.337
100000021 34 2011-10-28 16:52:03.360
100000022 35 2011-10-28 16:52:04.370
100000014 36 2011-10-28 16:51:56.250
*/
--SQL2005以上版本
select [ID],[SN],[RecordTime]
from (select *,row_number()over(partition by SN order by RecordTime desc) as row from @T) t
where row=1 |
|