急,求一SQL语句
一个表里有多条记录,有一ID字段和Time字段,一个ID可能有几条记录,怎么列出表里所有ID的记录,每个ID只能列出一条最新的记录。
------解决方案--------------------select id from 表 group by id having max(time)
------解决方案--------------------select * from tablename a
where not exists (select 1 from tablename where id = a.id and time > a.time)
------解决方案--------------------select t.* from 表 t where not exists(select 1 from 表 where ID=t.ID and Time> t.Time)
------解决方案--------------------select t.* from 表 t where t.time=(select max(time) from 表 where ID=t.ID)
------解决方案--------------------SELECT [ID],MAX([TIME])
FROM TABLENAME
GROUP BY [ID]
------解决方案--------------------select t.* from 表 t where t.time=(select top 1 time from 表 where ID=t.ID order by time desc)
------解决方案--------------------select
a.*
from
表 a,
(select ID,max(Time) as Time from 表 group by ID) b
where
a.ID=b.ID and a.Time=b.Time
------解决方案--------------------select * from tbName as tmp
where not exists(select 1 from tbName where ID=tmp.ID and Time> tmp.Time)
------解决方案--------------------select id,max(time) from 表 group by id