select top 20 * (
select avg(a.ps1_qty),b.pro_code from p_sale a,product b where a.pro_id=b.pro_id group by b.pro_code)不好意思,我是新手,不知道对不对,还希望高手们找找毛病

解决方案 »

  1.   

    select top 20 sum(a.ps1_qty),b.pro_code from p_sale a,product b where a.pro_id=b.pro_id group by b.pro_code order by sum(a.ps1_qty) desc
      

  2.   

    这句中能显示所有产品中销售量在前20名的。如果要每总产品的前20名,要写存储过程了。select top 20 *  from (select avg(a.ps1_qty) as AvgPs1Qty,b.pro_code from p_sale a,product b where a.pro_id=b.pro_id group by b.pro_code) A Order by A.AvgPs1Qty
      

  3.   

    对了,楼主是要总和还是平均呀,
    总和用sum
    平均用avg
      

  4.   

    select top 20 sum(a.psl_qty) as T from p_sale a,product b group by b.pro_code order by T desc
      

  5.   

    ---每个商品类别中销售前20名的商品、及其销售数量。
    select top 20  b.pro_code,sum(a.psl_qty)
      from p_sale a, product b
      where a.pro_id=b.pro_id
      group by pro_id 
      order by sum(a.psl_qty) desc
      

  6.   


    select b.pro_code,a.pro_id,sum(a.psl_qty) as psl_qty into #t from p_sale a,product b
    where a.pro_id=b.pro_id group by b.pro_code,a.pro_id
    order by b.pro_codeselect * from #t a where (select count(*) from #t where pro_code=a.pro_code and psl_qty>=a.psl_qty) <= 20
    order by pro_code, psl_qty descdrop table #t
      

  7.   

    直接将豌豆生成的#t放到后面的sql语句中。
    或者将临时表建成一张视图调用都可以。
      

  8.   

    --用这个试试:
    select * from (select b.pro_code,a.pro_id,sum(a.psl_qty) as psl_qty  from p_sale a,product b
    where a.pro_id=b.pro_id group by b.pro_code,a.pro_id) a where (select count(*) from (select b.pro_code,a.pro_id,sum(a.psl_qty) as psl_qty  from p_sale a,product b
    where a.pro_id=b.pro_id group by b.pro_code,a.pro_id) b where b.pro_code=a.pro_code and b.psl_qty>=a.psl_qty) <= 20
    order by pro_code, psl_qty desc
      

  9.   


    select b.pro_code,a.pro_id,a.psl_qty
    from 
    (select a.pro_id,a.sum(psl_qty) as psl_qty,b.pro_code 
    from p_sale a,product b 
    where a.pro_id=b.pro_id
    group by a.pro_id,b.pro_code) a and a.pro_id  in 
    (select top 20 pro_id from 
    (select a.pro_id,a.sum(psl_qty) as psl_qty,b.pro_code 
    from p_sale a,product b where a.pro_id=b.pro_id
    group by a.pro_id,b.pro_code)  where pro_code= a.pro_code
    order by psl_qty desc)
    order by a.pro_id desc,a.psl_qty desc
      

  10.   

    to: yesterday2000(一笑而过)思路没问题,语法有问题
    select b.pro_code,a.pro_id,a.psl_qty
    from 
    (select a.pro_id,a.sum(psl_qty) as psl_qty,b.pro_code 
    from p_sale a,product b 
    where a.pro_id=b.pro_id
    group by a.pro_id,b.pro_code) a where a.pro_id  in 
    (select top 20 pro_id from 
    (select a.pro_id,a.sum(psl_qty) as psl_qty,b.pro_code 
    from p_sale a,product b where a.pro_id=b.pro_id
    group by a.pro_id,b.pro_code) t where pro_code= a.pro_code
    order by psl_qty desc)
    order by a.pro_id desc,a.psl_qty desc