建立表结构
create table a
(sname int,    smonth int,   score int)      /*  分别是姓名,月份,分数*/
insert into a values ( 1,7,80)
insert into a values ( 1,8,40)
insert into a values ( 1,9,80)
insert into a values ( 2,7,50)
insert into a values ( 2,8,40)
insert into a values ( 2,9,60)
insert into a values ( 3,7,50)
insert into a values ( 3,8,80)
insert into a values ( 3,9,60)
insert into a values ( 4,7,50)
insert into a values ( 4,8,40)
insert into a values ( 4,9,60).............................对名字排序的对应表 (排序表)
 sort          sname
 1             3
 2             4
 3             1
 4             2    对每人的季度总分排名  然后再排序 ,排名已经写出来了,但排序我写不出来 
,排名如下:
select sname,
[4月]=sum(case when smonth=4 then score else 0 end),
[5月]=sum(case when smonth=5 then score else 0 end),
[6月]=sum(case when smonth=6 then score else 0 end),
[总分]=sum(score),
[排名]=( select count(*)+1 from(
select col=1 from a where smonth in(4,5,6) group by sname having sum(score)>sum(tmpA.score)
) tmp
)
from a tmpA
where smonth in(4,5,6)
group by sname
  
再在里面加上排序怎么写 ~~~
 谢谢

解决方案 »

  1.   

    我试图改
       from a tmpA
    where smonth in(4,5,6)
    group by sname
    为 
       from a tmpA  inner join 排序表 b on tmpA.sname=b.sname 
    where smonth in(4,5,6)
    group by sname
      order by b.sort
     结果不正确 ,不能通过,把 排名 那列 去了可以通过,所以 错误在 排名列中 ,大家修改下,看怎么排序
      

  2.   

    大概是我描述不清楚 ,其实很简单,只是要大家修改下
    首先大家  不看第二个排序表
         我只对第一个表进行排名 
    select sname,
    [4月]=sum(case when smonth=7 then score else 0 end),
    [5月]=sum(case when smonth=8 then score else 0 end),
    [6月]=sum(case when smonth=9 then score else 0 end),
    [总分]=sum(score),
    [排名]=( select count(*)+1 from(
    select col=1 from a where smonth in(7,8,9) group by sname having sum(score)>sum(tmpA.score)
    ) tmp
    )
    from a tmpA
    where smonth in(7,8,9)
    group by sname
    代码是正确的 
      
        我想在这个结果上再对sname 进行排序 ,对sname排序的对于关系就是 排序表.sname=a.sname
      

  3.   

    使用到临时表就可以 了。如:
    SELECT A.*,B.总分,IDENTITY(int,1,1) AS [排名] INTO #T  FROM (
    SELECT sname 
    ,SUM(CASE smonth WHEN 7 THEN score ELSE 0 END) AS '7月'
    ,SUM(CASE smonth WHEN 8 THEN score ELSE 0 END) AS '8月'
    ,SUM(CASE smonth WHEN 9 THEN score ELSE 0 END) AS '9月'
    FROM a GROUP BY sname 
    ) AS A
    RIGHT OUTER JOIN 
    (SELECT TOP 100 PERCENT sname,SUM(score) AS 总分 FROM a GROUP BY sname ORDER BY SUM(score) DESC) AS B 
    ON B.sname=A.sname
    SELECT * FROM #T
    DROP TABLE #T
      

  4.   

    /*
    结果:
    sname 7月 8月 9月 总分 排名
    ------------------------------------------
    1 80 40 80 200 1
    3 50 80 60 190 2
    4 50 40 60 150 3
    2 50 40 60 150 4
    */
      

  5.   

    实在还看不懂 我的意思看
    http://community.csdn.net/Expert/topic/5347/5347505.xml?temp=.6514856
     说的很清楚
      

  6.   

    DVD_01(OK_008) 
      肯定要有 order by 啊
    是按照  排序表(不是要排名的那个表)的sort字段 排序,   你的好象没有用到  排名我已经做出来了
      

  7.   


    select sname,
    [4月]=sum(case when smonth=7 then score else 0 end),
    [5月]=sum(case when smonth=8 then score else 0 end),
    [6月]=sum(case when smonth=9 then score else 0 end),
    [总分]=sum(score),
    [排名]=( select count(*)+1 from(
    select col=1 from a where smonth in(7,8,9) group by sname having sum(score)>sum(tmpA.score)
    ) tmp
    )
    from a tmpA
    where smonth in(7,8,9)
    group by sname order by 6结果如下:不知道是否是你要的答案。
    sname       4月          5月          6月          总分          排名          
    ----------- ----------- ----------- ----------- ----------- ----------- 
    1           80          40          80          200         1
    3           50          80          60          190         2
    4           50          40          60          150         3
    2           50          40          60          150         3
      

  8.   

    我哭 ~~
    不是按照  排名  1 2  3  4  啊
      我建立一个表吧  ~~~
    create table b ( id int,  sname int )
    insert into b values ( 1,4)
    insert into b values ( 2,2)
    insert into b values ( 3,1)
    insert into b values ( 4,3)   要求上面    zhfpoet() 的结果都按照  ID  排序,肯定要有对b表的连接啊~~
     我试了 ,一般的连接不行,  出错在排名上面 ,要修改 排名那个地方 
      

  9.   

    我做了一下,不知道是不是你要的结果,你看看吧:
    表脚本如下:
    create table a
    (sname int,    smonth int,   score int)      /*  分别是姓名,月份,分数*/
    insert into a values ( 1,7,80)
    insert into a values ( 1,8,40)
    insert into a values ( 1,9,80)
    insert into a values ( 2,7,50)
    insert into a values ( 2,8,40)
    insert into a values ( 2,9,60)
    insert into a values ( 3,7,50)
    insert into a values ( 3,8,80)
    insert into a values ( 3,9,60)
    insert into a values ( 4,7,50)
    insert into a values ( 4,8,40)
    insert into a values ( 4,9,60)
    gocreate table b
    (
    sort int,
    name int
    )
    go
    insert into b
    select 1,3
    union all 
    select 2,4
    union all
    select 3,1
    union all
    select 4,2go
    运行查询的脚本如下:
    select sname,
    [4月]=sum(case when smonth=7 then score else 0 end),
    [5月]=sum(case when smonth=8 then score else 0 end),
    [6月]=sum(case when smonth=9 then score else 0 end),
    [总分]=sum(score),
    [排名]=( select count(*)+1 from
    (
    select col=1 
    from a 
    where smonth in(7,8,9) 
    group by sname 
    having sum(score)>sum(tmpA.score)
    ) tmp
    )
    from a tmpA
    inner join b
    on b.name = tmpA.sname
    where smonth in(7,8,9)
    group by sname
    order by max(sort)结果如下:sname       4月          5月          6月          总分          排名
    ----------- ----------- ----------- ----------- ----------- -----------
    3           50          80          60          190         2
    4           50          40          60          150         3
    1           80          40          80          200         1
    2           50          40          60          150         3不知道符合要求不.
      

  10.   

    晕哦,好了,就是多个表而已,没关系,还是使用到临时表的方法,速度按道理上比使用子查询快。/*
    --新建的排序表:
    CREATE  TABLE T(sort int,sname int )
    INSERT INTO T
    SELECT  1      ,       3 UNION ALL
    SELECT  2      ,       4 UNION ALL
    SELECT  3      ,       1 UNION ALL
    SELECT  4      ,       2
    */SELECT A.*,B.总分,IDENTITY(int,1,1) AS [排名] INTO #T  FROM (
    SELECT sname 
    ,SUM(CASE smonth WHEN 7 THEN score ELSE 0 END) AS '7月'
    ,SUM(CASE smonth WHEN 8 THEN score ELSE 0 END) AS '8月'
    ,SUM(CASE smonth WHEN 9 THEN score ELSE 0 END) AS '9月'
    FROM a GROUP BY sname 
    ) AS A
    RIGHT OUTER JOIN 
    (SELECT TOP 100 PERCENT sname,SUM(score) AS 总分 FROM a GROUP BY sname ORDER BY SUM(score) DESC) AS B 
    ON B.sname=A.snameSELECT #T.* FROM #T
    LEFT OUTER JOIN T ON T.sname=#T.sname
    ORDER BY T.sort
    DROP TABLE #T
      

  11.   

    结果:
    sname       4月          5月          6月          总分          排名          
    ----------- ----------- ----------- ----------- ----------- ----------- 3 50 80 60 190 2
    4 50 40 60 150 3
    1 80 40 80 200 1
    2 50 40 60 150 4
      

  12.   

    不会把~~
      内部SQL 错误
      

  13.   

    scckobe(聪)   ~~
    第一个你的SQL 有点问题  我想要你第一个那样的 啊
     是有错误  我这真的不行   ~~~
    我开始也是那样做的   不行  ~~~
       你的可以  ?? 怪了