刚才已经提过一个类似问题了,但是现在新问题又来老~~
我现在有一张表,如:
wid 办事单位 得分数 处理文件 战法确认
 1     AA      5      3        串
 2     AA      6      2        并
 3     BB      3      1        串   
 4     CC      5      2        并
 5     CC      7      5        串
 6     BB      4      2        并
 7     AA      1      1        串想实现的结果如下:
序号 名次 办事单位 累计分数 处理文件数   串型
 1    1       AA     12        6          2
 2    1       CC     12        7          1
 3    3       BB     7         3          1就是说想以办事单位为group by 然后统计每个单位累计分数和处理文件数目
最后以累计分数来排序,以确定名次(注意:名字可能会出现分数相同的时候名次也相同),
还需要统计的就是每个单位的串型有多少个~~
能有拿位高手能帮帮忙吗~~~~在线等待中~

解决方案 »

  1.   

    if object_id('test') is not null drop table test
    select 1 as wid, 'AA' as 办事单位, 5 as 得分数, 3 as 处理文件, '串' as 战法确认
    into test
    union select 2, 'AA', 6, 2, '并'
    union select 3, 'BB', 3, 1, '串'
    union select 4, 'CC', 5, 2, '并'
    union select 5, 'CC', 7, 5, '串'
    union select 6, 'BB', 4, 2, '并'
    union select 7, 'AA', 1, 1, '串'
    -----------------------------------
    if object_id('tempdb..#') is not null drop table #select 办事单位, sum(得分数) as 累计分数, sum(处理文件) as 处理文件数, sum(case 战法确认 when '串' then 1 else 0 end) as 串型
    into #
    from test
    group by 办事单位select (select count(1) + 1 from # where 累计分数 > a.累计分数 or (累计分数 = a.累计分数 and 办事单位 < a.办事单位)) as 序号,
           (select count(1) + 1 from # where 累计分数 > a.累计分数) as 名次, a.*
    from # a
    order by 序号
    /*
    序号 名次 办事单位 累计分数 处理文件数 串型
    1    1    AA      12      6          2
    2    1    CC      12      7          1
    3    3    BB      7       3          1
    */
    drop table #
    -----------------------------------
    drop table test
      

  2.   

    select 办事单位,sum(得分数) as 得分数,sum(处理文件) as 处理文件,
    sum(case when 战法确认='串' then 1 else 0 end) as 串型
    into #
    from 一张表
    group by 办事单位select 1+(select count(*) from # where 得分数> a.得分数) as 名次,
    得分数,处理文件,串型
    from #a
    order by 得分数 desc序号就不加了
      

  3.   

    create table tbx(wid int, 办事单位 varchar(10), 得分数 int ,处理文件 int, 战法确认 varchar(2))
    insert into tbx select 
    1, 'AA', 5, 3, '串' union all select 
    2, 'AA', 6, 2, '并' union all select 
    3, 'BB', 3, 1, '串' union all select 
    4, 'CC', 5, 2, '并' union all select 
    5, 'CC', 7, 5, '串' union all select 
    6, 'BB', 4, 2, '并' union all select 
    7, 'AA', 1, 1, '串'
    go
    create view a
    as 
    (select 办事单位,sum(得分数) as 累计分数 ,sum(处理文件) as 处理文件数,sum(case when 战法确认 ='串' then 1 else 0 end) as 串型
    from tbx
    group by 办事单位
    ) select a.* ,(select count(*)+1 from a bx where bx.累计分数>a.累计分数 ) as 名次
    from a
    order by a.累计分数 desc
    办事单位       累计分数        处理文件数       串型          名次          
    ---------- ----------- ----------- ----------- ----------- 
    AA         12          6           2           1
    CC         12          7           1           1
    BB         7           3           1           3(所影响的行数为 3 行)