相同记录取最大值并且只显示一条 相关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 行受影响)
浙公网安备 33010602011771号