34,593
社区成员
发帖
与我相关
我的任务
分享
-- 建模拟数据
create table tb(ID int, Type int, Point int)
insert into tb select 1, 1, 20
union all select 2, 1, 30
union all select 3, 2, 10
union all select 4, 2, 15
union all select 5, 2, 15
-- 测试
select
*
from tb a
where not exists(select 1 from tb b where a.type = b.type and (a.point < b.point or (a.point = b.point and a.id > b.id)))
-- 结果
/*
ID Type Point
----------- ----------- -----------
2 1 30
4 2 15
(2 行受影响)
*/
select ID, Type, Point from
(select ID, Type, Point, row_number() over (partition by Type order by Point desc, ID) rid from tb) t
where rid=1;
select * from tb t where id=(select max(id) from tb where type=t.type)
selct id,type,max(point) from T A where (select top 1 id from T where type=A.type order by point desc)
group by id,type