表Test 字段(ID主键,WZMC,ShuLiang)  表中数据不止这两条数据,主要的用处是查询表中数据做个排行的,其中NO为自己as的,例如:   NO WZMC     ShuLiang
         1   鼠标     500
         2   鼠标     200合并后: NO WZMC     ShuLiang
         1  鼠标     700
我已写好的存储过程:
SELECT  ROW_NUMBER() OVER (ORDER BY ShuLiang DESC) AS NO,WZMC,SUM(ShuLiang)AS ShuLiang FROM dbo.Test where 1=1
麻烦各位大虾在这基础上帮我改下,谢啦

解决方案 »

  1.   


    SELECT ROW_NUMBER() OVER (ORDER BY ShuLiang DESC) AS NO,
    WZMC,
    SUM(ShuLiang)AS ShuLiang FROM dbo.Test
    group by WZMC
      

  2.   

    select min(no),WZMC,sum(ShuLiang) from Test group by WZMC
      

  3.   

    SELECT ROW_NUMBER() OVER (ORDER BY SUM(ShuLiang) DESC) AS NO,WZMC,SUM(ShuLiang)AS ShuLiang FROM dbo.Test where 1=1
      GROUP BY WZMC
      
      

  4.   

    select ROW_NUMBER() OVER (ORDER BY a.ShuLiang DESC) as NO,a.* from
    (select WZMC,SUM(ShuLiang) as ShuLiang from Test group by WZMC) as a
      

  5.   

    前面写的那个是查询全部的结果哇,1楼的哥们你的语句我在SQL2008里执行后报错。
    错误信息为:
    Msg 8120, Level 16, State 1, Line 1
    Column 'dbo.Test.ShuLiang' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
    Msg 8120, Level 16, State 1, Line 1
    Column 'dbo.Test.ShuLiang' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
      

  6.   

    附加下:
    ID int,
    WZMC nvarchar(50),
    ShuLiang int