日期:2014-05-18  浏览次数:20427 次

统计得分难题
一套测试,测试人员name列,测试难度THard有三个等级1、2、3,每人最多测试四次。
求根据Record表记录了各人通过测试的情况统计各人最终得分等级。

各人最终得分等级算法:
            1.取测试通过次数最多的难度等级(THard)为该人的最终得分等级。
                例:   A的难度等级1通过的次数为2,其他难度等级(2,3)测试通的次数都为1,A最终等级为2.
           
            2.当出现通过次数最多为2的难度等级有两个时,取两个难度中最难的为最终得分等级。
例:B的通过次数为2的难度等级为难度2和3,则B最终等级为3.

            3.当出现通过次数最多为1的难度等级有两个及以上时,取最低难度的为最终得分等级。
例:D通过难度2和3的次数都是1,D的最终等级为2.


Record表
----------------
ID     name     THard      
1         A         1
2         A         1
3         A         2
4         A         3
5         B         2
6         B         3
7         B         3
8         B         2
8         C         1
9         D         2
10       D         3
11       E         3
...   ...   ...

请各位帮忙解决一下,想了很久没想出来,谢谢!

------解决方案--------------------
A应该是1级吧。


--建立测试环境
create table Record(ID int,name varchar(10),THard int)
insert Record(ID,name,THard)
select '1 ', 'A ', '1 ' union all
select '2 ', 'A ', '1 ' union all
select '3 ', 'A ', '2 ' union all
select '4 ', 'A ', '3 ' union all
select '5 ', 'B ', '2 ' union all
select '6 ', 'B ', '3 ' union all
select '7 ', 'B ', '3 ' union all
select '8 ', 'B ', '2 ' union all
select '8 ', 'C ', '1 ' union all
select '9 ', 'D ', '2 ' union all
select '10 ', 'D ', '3 ' union all
select '11 ', 'E ', '3 '
go
--执行测试语句
create view v_record
as
select R.name,THard,count(1) as counts
from Record R
group by name,THard
go
select *
from v_record r
where not exists(
select 1 from v_record
where r.name = name
and(
r.counts < counts
or r.counts = 1 and counts = 1 and r.THard > THard
or r.counts > 1 and counts > 1 and r.counts = counts and r.THard < THard
)
)
go
--删除测试环境
drop table Record
drop view v_record
go
/*--测试结果
name THard counts
---------- ----------- -----------
A 1 2
B 3 2
C 1 1
D 2 1
E 3 1

(所影响的行数为 5 行)
*/



------解决方案--------------------
declare @Record table(ID int,name varchar(4),THard int)
insert into @Record values(1 , 'A ',1)
insert into @Record values(2 ,