求一条SQL语句,我晕死,几个大学生一起想几个小时想不出

vincent_1011 2008-07-16 11:58:20
有一个表 p1
数量 编号
50 1
70 1
30 2
80 3

//////////////////////////////

查找表p1中数量最多的编号,
给定一个格式,把问号部份补上

select 编号
from ??
??????????????????

...全文
505 52 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
52 条回复
切换为时间正序
请发表友善的回复…
发表回复
vincent_1011 2008-07-16
  • 打赏
  • 举报
回复
只有playwarcraft的答案才符合

那playwarcraft的最外层就是 select 编号 from T这样了?

刚刚发现playwarcraft语句中的 select 编号,數量=sum(數量) from p1 group by 编号
这里有点怪吧?》數量=sum(數量)是什么意思?
云海ldj 2008-07-16
  • 打赏
  • 举报
回复
select 編號 from p1
where 編號 IN (
select top 1 編號
form(select 编号,數量=sum(數量) from p1 group by 编号)A
order by 數量 desc)
sdxiong 2008-07-16
  • 打赏
  • 举报
回复
后面的T是一个别名,像

select ... (select ... from ..) T

这样,必须要给子查询的结果集给定一个别名
lff642 2008-07-16
  • 打赏
  • 举报
回复
你要的结果呢?要是数量最多的话.上面好些答案都满足你的要求.
vincent_1011 2008-07-16
  • 打赏
  • 举报
回复
分数打算给playwarcraft了,一下子楼给盖了,说错是楼上的了
vincent_1011 2008-07-16
  • 打赏
  • 举报
回复
汗死,原来from后面还可以加一个select这些语句,

什么垃圾学校,还没学到,不过括号后面要加T 什么的,这T是什么意思

估计要结贴了,哈,麻烦大家了,楼上的答下,我给你分

谢谢各位了
sdxiong 2008-07-16
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 vincent_1011 的回复:]
只能用一条SQL语句,不能声明变量,

5楼的也不行,因为注意这个表有点特殊,就是1号编号有2个,要把他们的数量加起来
[/Quote]



无语。。。。。

只是使用了表变量作测试,居然说不能。。。。晕
sdxiong 2008-07-16
  • 打赏
  • 举报
回复
create table p1 table (数量 int,编号 int)
insert into p1 select 50,1
insert into p1 select 30,1
insert into p1 select 70,2
insert into p1 select 80,3

select 编号
from p1
group by 编号
having sum(数量) = (select top 1 sum(数量) as 数量 from p1 group by 编号 order by 数量 desc)


/*
编号
-----------
1
3

(2 行受影响)
*/
playwarcraft 2008-07-16
  • 打赏
  • 举报
回复
那你要用那個,可以再加一層,o(∩_∩)o............汗死了

select 編號
from
(
select top 1 編號 form
(
select 编号,數量=sum(數量) from p1 group by 编号
)A order by 數量 desc
) T
playwarcraft 2008-07-16
  • 打赏
  • 举报
回复
。。。。。。。。。。。。。。。。
vincent_1011 2008-07-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yrwx001 的回复:]
SQL codeselect top 1 編號 form
(
select 编号,數量=sum(數量) from p1 group by 编号
)A order by 數量 desc
[/Quote]

原来也可以from的后面跟一个子查询?

OK,就说这个可以吧,不过这还不符合题意,

题里面的一开头就已经固定了,select 编号 from 这样的格式了

只能在from后面补上
vincent_1011 2008-07-16
  • 打赏
  • 举报
回复
只能用一条SQL语句,不能声明变量,

5楼的也不行,因为注意这个表有点特殊,就是1号编号有2个,要把他们的数量加起来
sdxiong 2008-07-16
  • 打赏
  • 举报
回复
应该是这样:

declare @tb table (数量 int,编号 int)
insert into @tb select 50,1
insert into @tb select 30,1
insert into @tb select 70,2
insert into @tb select 80,3

select 编号
from @tb
group by 编号
having sum(数量) = (select top 1 sum(数量) as 数量 from @tb group by 编号 order by 数量 desc)


/*
编号
-----------
1
3

(2 行受影响)
*/
fcuandy 2008-07-16
  • 打赏
  • 举报
回复
不知道你想问什么。 都说不清,难怪想不出。
yrwx001 2008-07-16
  • 打赏
  • 举报
回复
jf
sdxiong 2008-07-16
  • 打赏
  • 举报
回复
declare @tb table (数量 int,编号 int)
insert into @tb select 50,1
insert into @tb select 30,1
insert into @tb select 70,2
insert into @tb select 80,3

select 编号
from (select 编号,sum(数量) as 数量 from @tb group by 编号) a
WHERE 数量=(select max(数量) from @tb)

/*
编号
-----------
1
3

(2 行受影响)
*/
areswang 2008-07-16
  • 打赏
  • 举报
回复
select 编号
from test WHERE 数量=(select max(数量) from test)

????
wzy_love_sly 2008-07-16
  • 打赏
  • 举报
回复
create table tb(数量 int,编号 int)
insert into tb select 50,1
insert into tb select 29,1
insert into tb select 30,2
insert into tb select 80,3

select top 1 编号 from tb
group by 编号
order by sum(数量) desc


3
wzy_love_sly 2008-07-16
  • 打赏
  • 举报
回复
数量最多?
wzy_love_sly 2008-07-16
  • 打赏
  • 举报
回复
create table tb(数量 int,编号 int)
insert into tb select 50,1
insert into tb select 70,1
insert into tb select 30,2
insert into tb select 80,3

select top 1 编号 from tb
group by 编号
order by count(编号) desc


1
加载更多回复(32)

22,300

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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