新的问题又来拉~~50分~一个数据统计及排序问题,高手起来帮帮忙呀~~

ljjable 2006-05-12 04:34:33
刚才已经提过一个类似问题了,但是现在新问题又来老~~
我现在有一张表,如:
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 然后统计每个单位累计分数和处理文件数目
最后以累计分数来排序,以确定名次(注意:名字可能会出现分数相同的时候名次也相同),
还需要统计的就是每个单位的串型有多少个~~
能有拿位高手能帮帮忙吗~~~~在线等待中~
...全文
89 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
huailairen 2006-05-12
  • 打赏
  • 举报
回复
序号在程序里实现。
huailairen 2006-05-12
  • 打赏
  • 举报
回复
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 行)
昵称被占用了 2006-05-12
  • 打赏
  • 举报
回复
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

序号就不加了

dulei115 2006-05-12
  • 打赏
  • 举报
回复
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

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧