最后一个SQL  求一排序的SQL
建立表结构 
 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,90)   
 .............................   
    对每人的季度总分排名      然后再排序   ,排名已经写出来了,但排序我写不出来    
 ,排名如下: 
 select   sname, 
 [七月]=sum(case   when   smonth=7   then   score   else   0   end), 
 [八月]=sum(case   when   smonth=8   then   score   else   0   end), 
 [九月]=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   从上到下   1   ,2   ,3   ,4写出来的    
 sname            七月               八月            九月                     总分            排名 
 1	80	40	80	200	1 
 2	50	40	60	150	4 
 3	50	80	60	190	2 
 4	50	40	90	180	3     
    现在有另外一个表      对应上面的   1   ,2,3   ,4排序关系(不是排名), 
    id      sname          
       1            4          
       2            2 
       3            3 
       4            1 
 现在要求按照这个表的   ID   从上到下排(两个表sname是一样的   )       
 也就是想得到    
 sname            七月               八月            九月                     总分            排名 
 4	50	40	90	180	3 
 2	50	40	60	150	4 
 3	50	80	60	190	2 
 1	80	40	80	200	1      
 求SQL      谢谢   
------解决方案----------------------定义一个表变量 
  现在有另外一个表  对应上面的 1 ,2,3 ,4排序关系(不是排名), 
  id  sname    
   1    4    
   2    2 
   3    3 
   4    1 
 declare table @t(id int,sname int) 
 insert into @t 
 select 1,4 
 union all 
 select 2,2 
 union all 
 select 3,3 
 union all 
 select 4,1 
 --再和你以上的语句相联 
 select b.sname,a.七月,a.八月,a.九月,a.总分,a.排名 from @t b inner join  
 (select sname, 
 [七月]=sum(case when smonth=7 then score else 0 en