MSSQL语句实现排名次

use pubs

go

 

select *,(select count(distinct min_lvl) from jobs a where a.min_lvl >= b.min_lvl ) mingci from jobs b order by min_lvl desc

 

结果:

/*

job_id job_desc                                           min_lvl max_lvl mingci     
------ -------------------------------------------------- ------- ------- -----------
2      Chief Executive Officer                            200     250     1
3      Business Operations Manager                        175     225     2
4      Chief Financial Officier                           175     250     2
5      Publisher                                          150     250     3
6      Managing Editor                                    140     225     4
7      Marketing Manager                                  120     200     5
8      Public Relations Manager                           100     175     6
9      Acquisitions Manager                               75      175     7
10     Productions Manager                                75      165     7
11     Operations Manager                                 75      150     7
12     Editor                                             25      100     8
13     Sales Representative                               25      100     8
14     Designer                                           25      100     8
1      New Hire - Job not specified                       10      10      9

*/

 

select *,排名=( select count(*) from ( select min_lvl from jobs) as a where min_lvl>b.min_lvl ) +1
from ( select *  from jobs ) as b order by 排名

 

 

select *,(select count(min_lvl) from (select min_lvl from jobs) a where a.min_lvl>jobs.min_lvl)+1 as mincis from jobs order by mincis asc  --不连续排名

 

select *,(select count(distinct min_lvl) from (select min_lvl from jobs) a where a.min_lvl>jobs.min_lvl)+1 as mincis from jobs order by mincis asc --连续排名

 

/*

job_id job_desc                                           min_lvl max_lvl 排名         
------ -------------------------------------------------- ------- ------- -----------
2      Chief Executive Officer                            200     250     1
3      Business Operations Manager                        175     225     2
4      Chief Financial Officier                           175     250     2
5      Publisher                                          150     250     4
6      Managing Editor                                    140     225     5
7      Marketing Manager                                  120     200     6
8      Public Relations Manager                           100     175     7
9      Acquisitions Manager                               75      175     8
10     Productions Manager                                75      165     8
11     Operations Manager                                 75      150     8
12     Editor                                             25      100     11
13     Sales Representative                               25      100     11
14     Designer                                           25      100     11
1      New Hire - Job not specified                       10      10      14

*/

create   table   tb(考号   varchar(10),科目代号   varchar(10),得分   int)  
  insert   into   tb   select   '0001'   ,'01',   60  
  union   all   select   '0001'   ,'02',   80  
  union   all   select   '0001'   ,'03',   90  
  union   all   select   '0001'   ,'04',   100  
  union   all   select   '0002'   ,'01',   50  
  union   all   select   '0002'   ,'02',   40  
  union   all   select   '0002'   ,'03',   60  
  union   all   select   '0002'   ,'04',   90  
   
  declare   @sql   varchar(8000)  
  set   @sql='select   a.考号'  
  select   @sql=@sql+',[科目'+cast(科目代号   as   varchar)+']=sum(case   a.科目代号   when   '''+cast(科目代号   as   varchar)+'''   then   a.得分   else   0   end)'   from   tb   group   by   科目代号  
  exec(@sql+',sum(a.得分)   as   总分,identity(int,1,1)   as   名次   into   tmp   from   tb   a   group   by   a.考号   order   by   sum(a.得分)   desc')  
  select   *   from   tmp  
   
  drop   table   tb,tmp

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值