select dtempt.学号 ,dtempt.考试时间,dtempt.考次,dtempt.总分,ddtempt.最高分,ddtempt.平均分,dddtempt.排名  from (select StuID as 学号,总分=isNull(Computer,0)+isNull(Physic_A,0)+isNull(Physic_B,0)+isNull(History,0)+isNull(English,0)+isNull(Politic,0)+isNull(art,0)+isNull(Maths,0) , 考试时间=TestTime,考次=Testname from s) dtempt,
(select 考次,最高分=max(总分),平均分=avg(总分)
 from (select StuID as 学号,总分=isNull(Computer,0)+isNull(Physic_A,0)+isNull(Physic_B,0)+isNull(History,0)+isNull(English,0)+isNull(Politic,0)+isNull(art,0)+isNull(Maths,0) , 考试时间=TestTime,考次=Testname from s) dtempt group by 考次) ddtempt,
(select 学号,考次,(select count(distinct 总分) from (select StuID as 学号,总分=isNull(Computer,0)+isNull(Physic_A,0)+isNull(Physic_B,0)+isNull(History,0)+isNull(English,0)+isNull(Politic,0)+isNull(art,0)+isNull(Maths,0) , 考试时间=TestTime,考次=Testname from s) dtempt where 总分>=a.总分 and 考次=a.考次) as 排名
from (select StuID as 学号,总分=isNull(Computer,0)+isNull(Physic_A,0)+isNull(Physic_B,0)+isNull(History,0)+isNull(English,0)+isNull(Politic,0)+isNull(art,0)+isNull(Maths,0) , 考试时间=TestTime,考次=Testname from s) a) dddtempt where ddtempt.考次=dtempt.考次 and dtempt.考次=dddtempt.考次 and dddtempt.学号=dtempt.学号 order by dtempt.考次;

解决方案 »

  1.   

    建议:select StuID as 学号,总分=isNull(Computer,0)+isNull(Physic_A,0)+isNull(Physic_B,0)+isNull(History,0)+isNull(English,0)+isNull(Politic,0)+isNull(art,0)+isNull(Maths,0), 考试时间=TestTime,考次=Testname into #dtempt from s;
    select #dtempt.学号 ,#dtempt.考试时间,#dtempt.考次,#dtempt.总分,ddtempt.最高分,ddtempt.平均分,dddtempt.排名  from 
    #dtempt,(select 考次,最高分=max(总分),平均分=avg(总分) from #dtempt group by 考次) ddtempt,(select 学号,考次,(select count(distinct 总分) from #dtempt where 总分>=a.总分 and 考次=a.考次) as 排名 from #dtempt a) dddtempt where ddtempt.考次=#dtempt.考次 and #dtempt.考次=dddtempt.考次 and dddtempt.学号=#dtempt.学号 order by #dtempt.考次;
     
    drop table #dtempt;