有两个表。
表1单位表,两个字段,id,单位名称
表2信息表,主要字段:信息id,得分,单位id(即表1的id)现在想统计各单位信息得分的总和并根据总和进行排名。比如名次 单位名称 得分
1   销售部   56
2   综合部   43
2   后勤部   43
3   市场部   25请高手帮忙,求详细代码。谢谢!

解决方案 »

  1.   

    排名函数 DENSE_RANK ( )
      

  2.   

    create table tb1 (ID int,unit_name varchar(100))
    go
    create table tb2 (msgid int,score int,unit_id int)
    goinsert into tb1 (ID,unit_name ) 
    select 1,'销售部' union all
    select 2,'综合部' union all
    select 3,'后勤部' union all
    select 4,'市场部'
    go
    insert into tb2 (msgid,score,unit_id )
     select null,56,1 union all
     select null,43,2 union all
     select null,43,3 union all
     select null,25,4
     
     
     select DENSE_RANK() over (order by sum(tb2.score)) as mingci,tb1.unit_name ,sum(tb2.score) as score
    from tb1 join tb2 on tb1.ID=tb2.unit_id 
    group by tb1.unit_name 
     
      

  3.   

    你这表结构里面也没有年份或日期字段,如果有的话在 where条件里面加入年份=2013