读书人

求查询为最大值的记录,该怎么解决

发布时间: 2012-04-22 18:34:46 作者: rapoo

求查询为最大值的记录
declare @t table
(
id int,
name varchar(1000),
addtime varchar(20)
)

insert @t select 1,'a','2012-02-06'
union all select 2,'a','2012-06-21'
union all select 3,'a','2012-04-05'
union all select 4,'b','2012-09-21'
union all select 5,'b','2012-02-01'

select * from @t

--查询增加时间为最大值的记录如下:
-- id name addtime
-- 2 a 2012-06-21
-- 4 b 2012-09-21

[解决办法]

SQL code
select * from @t t   where not exists(select 1 from @t where name=t.name and addtime>t.addtime) 

读书人网 >SQL Server

热点推荐