select top 1 編號 form
(
select 编号,數量=sum(數量) from p1 group by 编号 
)A order by 數量 desc

解决方案 »

  1.   

    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,3select top 1 编号 from tb
    group by 编号
    order by count(编号) desc1
      

  2.   

    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,3select top 1 编号 from tb
    group by 编号
    order by sum(数量) desc3
      

  3.   

    select 编号 
    from test WHERE 数量=(select max(数量) from test)????
      

  4.   

    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,3select 编号 
    from (select 编号,sum(数量) as 数量 from @tb group by 编号)  a
    WHERE 数量=(select max(数量) from @tb) /*
    编号
    -----------
    1
    3(2 行受影响)
    */
      

  5.   

    应该是这样: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,3select 编号
    from @tb
    group by 编号
    having sum(数量) = (select top 1 sum(数量)  as 数量 from @tb group by 编号 order by 数量 desc)
    /*
    编号
    -----------
    1
    3(2 行受影响)
    */
      

  6.   

    只能用一条SQL语句,不能声明变量,5楼的也不行,因为注意这个表有点特殊,就是1号编号有2个,要把他们的数量加起来
      

  7.   


    原来也可以from的后面跟一个子查询?OK,就说这个可以吧,不过这还不符合题意,题里面的一开头就已经固定了,select 编号 from 这样的格式了只能在from后面补上
      

  8.   

    那你要用那個,可以再加一層,o(∩_∩)o............汗死了select 編號
    from
    (
    select top 1 編號 form 

    select 编号,數量=sum(數量) from p1 group by 编号 
    )A order by 數量 desc 
    ) T
      

  9.   

    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) 
    /* 
    编号 
    ----------- 

    3 (2 行受影响) 
    */
      

  10.   

    汗死,原来from后面还可以加一个select这些语句,什么垃圾学校,还没学到,不过括号后面要加T 什么的,这T是什么意思估计要结贴了,哈,麻烦大家了,楼上的答下,我给你分谢谢各位了
      

  11.   

    分数打算给playwarcraft了,一下子楼给盖了,说错是楼上的了
      

  12.   

    后面的T是一个别名,像select ... (select ... from ..) T这样,必须要给子查询的结果集给定一个别名
      

  13.   

    select 編號 from p1 
    where 編號 IN (
      select top 1 編號 
       form(select 编号,數量=sum(數量) from p1 group by 编号)A 
      order by 數量 desc)
      

  14.   

    只有playwarcraft的答案才符合那playwarcraft的最外层就是 select 编号 from T这样了?刚刚发现playwarcraft语句中的 select 编号,數量=sum(數量) from p1 group by 编号 
    这里有点怪吧?》數量=sum(數量)是什么意思?
      

  15.   

    很明显,playwarcraft的运行结果只有一条记录,而正确的答案是有两条记录的。楼主,先运行一下再下结论吧。
      

  16.   


    恩,是啊,那如果是这样,那应该怎么写了呢?不用declare 一些变量出来。
      

  17.   

    select 编号 from p1 where max(数量) group by 编号!
      

  18.   

    这是给的样例数据,并没有说明和没有相等的,你就必须考虑到,如果只从所给的,你直接用
    select 编号 from p1 where 编号=1 不是更简单?
      

  19.   

    一群人在这个问题上折腾,LZ赶快结贴,哈哈,别听他们蛊惑。p.s:你们学校的大学生,和我当年一个样。
      

  20.   

    --最直观的,不知上面有吗,其它方法大家想
    select 编号 
    from p1 a group by 编号 
    having sum(数量)=(select max(num) from (select sum(数量) num from p1 group by 编号)A)
      

  21.   

    我在14楼就已经有答案了select 编号 
    from p1 
    group by 编号 
    having sum(数量) = (select top 1 sum(数量)  as 数量 from p1 group by 编号 order by 数量 desc) 
      

  22.   

    20分这么多人,本不想进,这不看到sdxiong发现问题了吗,指出了你又不改
      

  23.   

    select TOP1 编号,SUM(数量) from 表 GROUP BY 编号  
    好像是 没有测试过
      

  24.   

    select top 1 编号,SUM(数量) from 表 GROUP BY 编号 order by sum(数量)  desc
    呵呵 测试过 这样写 刚才忘了排序
      

  25.   

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

  26.   

    select top 1 sno from 
    (
    select sno,sum(quantity) as sumquantity
    from 
    (
    select 50 as quantity,1 as sno 
    union all
    select 70 as quantity,1 as sno 
    union all
    select 30 as quantity,2 as sno 
    union all
    select 80 as quantity,3 as sno 
    union all
    select 50 as quantity,4 as sno 
    )  as tb
    group by sno
    ) as tb1 
    order by sumquantity desc
      

  27.   

    www.dullwolf.cn/chess/ 
    和我下棋吧
      

  28.   

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

  29.   

    select 编号 from (select top 1 编号 from p1 group by 编号   order by count(*) desc) c