日期:2014-05-18  浏览次数:20659 次

在线求一SQL语句,马上结贴,谢谢!
表内容如下:
  ID             SSC                 Time
--------     -----       -----------------------
ESP00001 1 2007-08-14   14:55:18.000
ESP00002 1 2007-08-20   14:27:08.417
INT00001 2 2007-08-20   15:37:47.060
INT00001 3 2007-08-20   16:53:11.280
ESP00003 4 2007-08-20   17:16:02.060
ESP00001 5 2007-08-20   18:09:27.983
ESP00001 6 2007-08-20   18:09:53.297
ESP00003 10 2007-08-21   18:36:52.287
ESP00004 5 2007-08-22   16:50:33.413
ESP00003 9 2007-08-23   15:30:49.850
MCI00001 1 2007-08-23   17:19:36.807
ESP00003 10 2007-08-23   17:22:23.087
ESP00003 9 2007-08-24   11:12:14.973

我想查出来的结果如下:
  ID             SSC                 Time
--------     -----       -----------------------
ESP00001   6                 2007-08-20   18:09:53.297
ESP00002   1                 2007-08-20   14:27:08.417
ESP00003   9                 2007-08-24   11:12:14.973
ESP00004   5                 2007-08-22   16:50:33.413
INT00001   3                 2007-08-20   16:53:11.280
MCI00001   1                 2007-08-23   17:19:36.807

就是:查出ID相同,但是时间最大的一条记录,但是不允许ID重复,不知道我说清楚了没?谢谢大家!

------解决方案--------------------
--这样?
select *
from 表名 as T
where [Time]=(select max([time]) from 表名 where id=T.id)
------解决方案--------------------
----方法1
select * from table as a where not exists(select 1 from table where ID = a.ID and Time > a.Time)
----方法2
select * from table as a where Time = (select top 1 Time from table where ID = a.ID order by Time DESC)
----方法3
select * from table as a where Time = (select max(Time) from table where ID = a.ID)


------解决方案--------------------
Select ID, Max(Time) Time from table
Group by ID
Order by ID