表
ID name time 1 a 2009 2 a 2012 3 b 2007 4 b 20095 b 20126 c 2009
==================================
结果id name time2 a 20125 b 20126 c 2009怎么查询 出来的结果是 过滤掉 重复 的name 获取最新时间 的那条数据
ID name time 1 a 2009 2 a 2012 3 b 2007 4 b 20095 b 20126 c 2009
==================================
结果id name time2 a 20125 b 20126 c 2009怎么查询 出来的结果是 过滤掉 重复 的name 获取最新时间 的那条数据
from 表 a
where exists (select 1 from (select name,max(time) time from 表 group by name) b
where a.name=b.name and a.time=b.time)
select a.id,a.name,a.time from tbName a,
(select id,max(time) as time from tbName group by name) b
where a.id = b.id and a.time = b.time;
tbName 就是你存数据的表名
select 'a','2009' union
select 'a','2012' union
select 'b' ,'2007'union
select 'b' ,'2009'union
select 'b', '2012'union
select 'c', '2009'select * from #select * from (
select ROW_NUMBER() OVER(PARTITION BY name ORDER BY time desc ) as uid ,* from #
) b
where uid = 1
select 'a','2009' union
select 'a','2012' union
select 'b' ,'2007'union
select 'b' ,'2009'union
select 'b', '2012'union
select 'c', '2009'select * from #select a.ID,a.name,a.time from # a,(select name,max(time) as time from # group by name)b where
a.name=b.name and b.time=a.time order by a.ID