创建:
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个成绩
执行结果:
只用SQL Server?输出倒还好办?输入呢?或许可以用XML文件的方式保存输入数据,即选手和打分。
创建一个存储过程,参数是一个长字符串,用于录入xml
然后在存储过程中打开这个xml,存储到临时表里(OPENXML)
针对每个选手的打分去掉最高和最低
计算平均分(group by ,avg等 )