34,590
社区成员
发帖
与我相关
我的任务
分享
select * from 表名 a where id in (select top 3 id from 表名 where Group=a.Group order by amount desc)
create table #T (ID int,[Group] varchar(2),Sales varchar(8),Amount int)
insert into #T
select 1,'A','Sales1',100 union all
select 2,'A','Sales2',120 union all
select 3,'A','Sales3',105 union all
select 4,'A','Sales4',103 union all
select 5,'B','Sales1',203 union all
select 6,'B','Sales2',250 union all
select 7,'B','Sales5',300 union all
select 8,'B','Sales6',301
select * from #T a where id in (select top 3 id from #T where [Group]=a.[Group] order by Amount desc)
/*
--------------------------
2 A Sales2 120
3 A Sales3 105
4 A Sales4 103
6 B Sales2 250
7 B Sales5 300
8 B Sales6 301
*/