请帮忙拼一个sql句子

KAI3000 2004-05-07 03:53:41

一个表两个字段a,b,数据形式如下
字段a 字段b
sfs 1
sas 1
sgs 1
sts 1
sfs 2
sfs 2
sfs 2
sfs 2
sfs 3
sfs 3
sfs 3
sfs 3
sfs 4
sfs 4
……
……
afs n
sfa n
sft n
怎么写sql句子,取出b分别=1,2,3,4,...n时的top m共m*n个rows?

有人提议用union但,n大于10岂不要写n-1个union?麻烦
...全文
40 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 元老 2004-05-08
  • 打赏
  • 举报
回复
select id=identity(int,1,1),* into #t from 表 --表要改为你的表名

select * from #t a --此处的a,是表#t的别名
where id in(
select top 4 id from #t where b=a.b --此处的a,是外层的那个#t(通过别名引用),b是字段名
)
drop table #t
zjcxc 元老 2004-05-08
  • 打赏
  • 举报
回复
同意 hmily1688(刘涛--公司里面打杂的)

语句写法可以搬,字段名,表名得根据你自己的情况调整
Frewin 2004-05-08
  • 打赏
  • 举报
回复
up
hmily1688 2004-05-08
  • 打赏
  • 举报
回复
你的当然出错了,邹老大的是测试数据,你得把a b类的换成你自己的字段名,
KAI3000 2004-05-07
  • 打赏
  • 举报
回复
我把zjcxc(邹建)的拷贝到sql查询分析器里面去执行,

居然出错?


(所影响的行数为 17 行)

服务器: 消息 207,级别 16,状态 3,行 5
列名 'b' 无效。
服务器: 消息 207,级别 16,状态 1,行 5
列名 'b' 无效。
KAI3000 2004-05-07
  • 打赏
  • 举报
回复
好,刚才没看到回复,这次我实践一下。谢谢各位。
KAI3000 2004-05-07
  • 打赏
  • 举报
回复
谢谢楼上的两位,但我没看懂

是这样的

表名为 myTable,
字段分别为a,b。

我看你们俩人的句子,看晕了。a到底是表还是字段?
zh_zh_y 2004-05-07
  • 打赏
  • 举报
回复
我来学习
zjcxc 元老 2004-05-07
  • 打赏
  • 举报
回复
--测试

--测试数据

create table 表(a varchar(10),b int)
insert 表 select 'sfs',1
union all select 'sas',1
union all select 'sgs',1
union all select 'sts',1
union all select 'sfs',2
union all select 'sfs',2
union all select 'sfs',2
union all select 'sfs',2
union all select 'sfs',3
union all select 'sfs',3
union all select 'sfs',3
union all select 'sfs',3
union all select 'sfs',4
union all select 'sfs',4
union all select 'afs',4
union all select 'sfa',4
union all select 'sft',4
go

--查询,假设m=4

select id=identity(int,1,1),* into #t from 表
select * from #t a where id in(select top 4 id from #t where b=a.b)
drop table #t
go

--删除测试
drop table 表

/*--测试结果

id a b
----------- ---------- -----------
1 sfs 1
2 sas 1
3 sgs 1
4 sts 1
5 sfs 2
6 sfs 2
7 sfs 2
8 sfs 2
9 sfs 3
10 sfs 3
11 sfs 3
12 sfs 3
13 sfs 4
14 sfs 4
15 afs 4
16 sfa 4

(所影响的行数为 16 行)
--*/
hmily1688 2004-05-07
  • 打赏
  • 举报
回复
假设你的
create procedure mypro(@m int,@n int)
as
begin
declare @sql varchar(8000)
declare @temp int
set @temp=0
set @sql=N''
while (@temp<@n)
begin
select @temp=@temp+1
select @sql=@sql+' select top '+cast(@m as varchar)+' * from a where bbb='+cast(@temp as varchar)+' Union'
end
select @sql=substring(@sql,1,len(@sql)-5);
exec(@sql)
end ;
hmily1688 2004-05-07
  • 打赏
  • 举报
回复
好像不用union是不行的吧,楼主要取的是top m 共m*n个数据,
也就是说当字段b=1时取下top m,字段b=2时取下top m,以此类推是吧。
hmily1688 2004-05-07
  • 打赏
  • 举报
回复
看不懂中海的语句,嘻嘻哈哈.
zjcxc 元老 2004-05-07
  • 打赏
  • 举报
回复
--假设你的m为4

select id=identity(int,1,1),* into #t from 你的表

select * from #t a where id in(select top 4 id from #t where b=a.b)

drop table #t
victorycyz 2004-05-07
  • 打赏
  • 举报
回复

select id=identity(int,1,1),* into #t from tablename order by b

select a.a,a.b
from #t a inner join
( selecct b, min(id) as id
from #t
group by b
) b
on a.b=b.b and a.id<b.id+10

drop table #t

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧