62,267
社区成员
发帖
与我相关
我的任务
分享declare @A table(a int, b varchar(2), c nchar(2))
insert into @A
select 1 ,'sf','t1' union all
select 2 ,'s','t1' union all
select 3 ,'dd','t2' union all
select 4 ,'dd','t3' union all
select 6 ,'dd','t6' union all
select 7 ,'dd','t2'
select top 4 * from @A where c in
(select top 2 c from @A group by c)
a b c
----------- ---- ----
1 sf t1
2 s t1
3 dd t2
7 dd t2
(4 行受影响)create table tb
(
a int,
b nvarchar(50),
c nvarchar(50),
)
insert tb select 1,'sf','t1'
insert tb select 2,'s','t1'
insert tb select 3,'dd','t2'
insert tb select 4,'dd','t3'
insert tb select 6,'dd','t6'
select * from tb t where a in
(select top 2 a from tb where c=t.c)
drop table tb
(1 個資料列受到影響)
(1 個資料列受到影響)
(1 個資料列受到影響)
(1 個資料列受到影響)
(1 個資料列受到影響)
a b c
----------- -------------------------------------------------- --------------------------------------------------
1 sf t1
2 s t1
3 dd t2
4 dd t3
6 dd t6
(5 個資料列受到影響)
declare @A table(a int, b varchar(2), c nchar(2))
insert into @A
select 1 ,'sf','t1' union all
select 2 ,'s','t1' union all
select 3 ,'dd','t2' union all
select 4 ,'dd','t3' union all
select 6 ,'dd','t6' union all
select 7 ,'dd','t2' union all
select 8 ,'cc','t1' union all
select 9 ,'a','t2' union all
select 10 ,'s','t2'
select * from @A m where a in
(select top 2 a from @A where c=m.c)
order by m.c
a b c
----------- ---- ----
1 sf t1
2 s t1
3 dd t2
7 dd t2
4 dd t3
6 dd t6
(6 行受影响)