62,254
社区成员
发帖
与我相关
我的任务
分享
select * from
(
select a.*,row_number()over(partition by 地区编号 order by 编号 ) as num from ta a
) a
where num<=2
select * from [表名] as dqTable
where [编号] in
(select top 2 [编号] from dqTable where [地区编号]=dqTable.[地区编号]
order by [编号])
order by [地区],[编号]
declare @t table(编号 int,商圈名称 varchar(4),地区编号 varchar(4))
insert @t select 1,'a','010'
union all select 2,'b','010'
union all select 4,'c','010'
union all select 5,'d','010'
union all select 6,'a','021'
union all select 7,'b','021'
union all select 8,'c','021'
select * from @t
select * from @t a
where 编号 in
(
select top 2 编号
from @t
where 地区编号=a.地区编号
order by 编号
)
select * from Test_table a where [编号] in (select top 2 [编号] from Test_table where [地区编号]=a.[地区编号])
declare @t table(id int,col varchar(4),code varchar(4))
insert @t select 1,'a','010'
union all select 2,'b','010'
union all select 4,'c','010'
union all select 5,'d','010'
union all select 6,'f','021'
select * from @t
select * from @t a
where id in
(
select top 2 id
from @t
where code=a.code
order by id
)
/*
(5 行受影响)
id col code
----------- ---- ----
1 a 010
2 b 010
4 c 010
5 d 010
6 f 021
(5 行受影响)
id col code
----------- ---- ----
1 a 010
2 b 010
6 f 021
(3 行受影响)
*/