首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

最后一个SQL 求一排序的SQL,该如何解决

2012-02-05 
最后一个SQL求一排序的SQL建立表结构createtablea(snameint,smonthint,scoreint)/*分别是姓名,月份,分数*/

最后一个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         七月           八月         九月               总分         排名
18040802001
25040601504
35080601902
45040901803


  现在有另外一个表     对应上面的   1   ,2,3   ,4排序关系(不是排名),
  id     sname      
    1         4      
    2         2
    3         3
    4         1
现在要求按照这个表的   ID   从上到下排(两个表sname是一样的   )    
也就是想得到  
sname         七月           八月         九月               总分         排名
45040901803
25040601504
35080601902
18040802001
 
求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 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
) a
on b.id=a.sname
order by b.sname desc
[解决办法]
--排名的寫法稍改下成不?

select T.* from
(
select tmpA.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(*) from (select sname,sum(score) as score from a group by sname) t1
where t1.score> =sum(tmpA.score)
)
from a tmpA
where smonth in(7,8,9)
group by tmpA.sname) T
inner join b on T.sname=b.sname
order by b.id

/*
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
*/

热点排行
Bad Request.