在SQL Server中,编写竞赛用评分程序,由10个评委给选手打分,去掉一个最高分和一个最低分,

2025-03-10 18:39:12
推荐回答(2个)
回答1:

创建:

create proc p_avg_score
(@score1 decimal(4,2),
@score2 decimal(4,2),
@score3 decimal(4,2),
@score4 decimal(4,2),
@score5 decimal(4,2),
@score6 decimal(4,2),
@score7 decimal(4,2),
@score8 decimal(4,2),
@score9 decimal(4,2),
@score10 decimal(4,2))--定义10个输入参数
as
declare @avg_score decimal(4,2) --定义平均值
declare @max_score decimal(4,2) --定义最高分数
declare @min_score decimal(4,2) --定义最低分数
select @max_score=MAX(score) from 
(select @score1 as score 
union all
select @score2 
union all
select @score3 
union all
select @score4 
union all
select @score5 
union all
select @score6 
union all
select @score7
union all
select @score8 
union all
select @score9 
union all
select @score10) t  --获取分数中的最高分

select @min_score=min(score) from
(select @score1 as score 
union all
select @score2 
union all
select @score3 
union all
select @score4 
union all
select @score5 
union all
select @score6 
union all
select @score7
union all
select @score8 
union all
select @score9 
union all
select @score10) t  --获取分数中的最低分

select @avg_score=round((SUM(score)-@min_score-@max_score+0.00)/8,2) from
(select @score1 as score 
union all
select @score2 
union all
select @score3 
union all
select @score4 
union all
select @score5 
union all
select @score6 
union all
select @score7
union all
select @score8 
union all
select @score9 
union all
select @score10) t where score not in (@min_score,@max_score) --去掉一个最高分和一个最低分后的平均分
print @avg_score --打印成绩

执行:

exec p_avg_score 1,2,3,4,5,6,7,8,9,4

其中1,2,3,4,5,6,7,8,9,4为输入的10个成绩

执行结果:

回答2:

只用SQL Server?输出倒还好办?输入呢?或许可以用XML文件的方式保存输入数据,即选手和打分。

  1. 创建一个存储过程,参数是一个长字符串,用于录入xml

  2. 然后在存储过程中打开这个xml,存储到临时表里(OPENXML)

  3. 针对每个选手的打分去掉最高和最低

  4. 计算平均分(group by ,avg等 )