相同记录取最大值并且只显示一条 相关sql

declare @tb table(id int, code varchar(10), [values] int)
insert @tb select
1, 'a' , 2 union all select
2 , 'a' , 3 union all select
3 , 'a' , 3 union all select
4 , 'b' , 1 union all select
5 ,'b' , 4 union all select
6 ,'b' , 4


select* from @tb t
where not exists (select * from @tb where code=t.code and ([values]>t.[values] or [values]=t.[values] and id>t.id))

id code values
----------- ---------- -----------
3 a 3
6 b 4

(2 行受影响)

posted on 2011-10-08 09:54  卡域克  阅读(414)  评论(0)    收藏  举报