2,596
社区成员
发帖
与我相关
我的任务
分享
create table t1( a varchar(8),b varchar(8), c int)
insert into t1
select 'aa', 'sx', 1000 union
select 'aa', 'zh', 900 union
select 'bb', 'jj', 1100 union
select 'bb', 'zh', 500 union
select 'bb', 'gg', 400 union
select 'bb', 'wd', 300 union
select 'bb', 'sh', 150 union
select 'cc', 'sh', 1500
--select * From t1
select * from [t1] T where b in(
select top 3 b from [t1] where a=t.a order by [a]
) order by a
drop table t1
/*
(8 行受影响)
a b c
-------- -------- -----------
aa sx 1000
aa zh 900
bb gg 400
bb jj 1100
bb sh 150
cc sh 1500
(6 行受影响)
*/
select t.* from
(select Atable.*,row_number() over(partition by A order by C desc) rn from Atable) t
where rn<=3
select p.a.p.b.p.c
from ATable p inner join ATable q on p.a=q.a and ( p.c<q.c or (p.c=q.c and p.b<=q.b))
group p.a.p.b.p.c
having count(*)<=3