你可以参考这个:现在有一个表,cj(姓名,评委一,评委二,评委三,评委四,评委五)想把评委中打分最高与最低外的三者的平均分求出并按其高低排序,如何写sql?(1)测试数据 create table cj (姓名 nvarchar(50), 评委一 int, 评委二 int, 评委三 int, 评委四 int, 评委五 int)insert cj select '张三',100,99,98,97,96 union all select '李四',99,98,97,96,95 union all select '王五',80,81,82,83,84 union all select '曾六',90,91,92,93,94(2)查询 select 姓名,avg(评委一+评委二+评委三+评委四+评委五)/5 as 平均分 from cj where 评委一 not in ( select max(分数) from ( select 姓名,max(评委一)as 分数 from cj group by 姓名 union all select 姓名,max(评委二)as 分数 from cj group by 姓名 union all select 姓名,max(评委三)as 分数 from cj group by 姓名 union all select 姓名,max(评委四)as 分数 from cj group by 姓名 union all select 姓名,max(评委五)as 分数 from cj group by 姓名 -- order by 姓名,分数 ) as X group by 姓名 ) or 评委二 not in ( select max(分数) from ( select 姓名,max(评委一)as 分数 from cj group by 姓名 union all select 姓名,max(评委二)as 分数 from cj group by 姓名 union all select 姓名,max(评委三)as 分数 from cj group by 姓名 union all select 姓名,max(评委四)as 分数 from cj group by 姓名 union all select 姓名,max(评委五)as 分数 from cj group by 姓名 -- order by 姓名,分数 ) as X group by 姓名 ) or 评委三 not in ( select max(分数) from ( select 姓名,max(评委一)as 分数 from cj group by 姓名 union all select 姓名,max(评委二)as 分数 from cj group by 姓名 union all select 姓名,max(评委三)as 分数 from cj group by 姓名 union all select 姓名,max(评委四)as 分数 from cj group by 姓名 union all select 姓名,max(评委五)as 分数 from cj group by 姓名 -- order by 姓名,分数 ) as X group by 姓名 ) or 评委四 not in ( select max(分数) from ( select 姓名,max(评委一)as 分数 from cj group by 姓名 union all select 姓名,max(评委二)as 分数 from cj group by 姓名 union all select 姓名,max(评委三)as 分数 from cj group by 姓名 union all select 姓名,max(评委四)as 分数 from cj group by 姓名 union all select 姓名,max(评委五)as 分数 from cj group by 姓名 -- order by 姓名,分数 ) as X group by 姓名 ) or 评委五 not in ( select max(分数) from ( select 姓名,max(评委一)as 分数 from cj group by 姓名 union all select 姓名,max(评委二)as 分数 from cj group by 姓名 union all select 姓名,max(评委三)as 分数 from cj group by 姓名 union all select 姓名,max(评委四)as 分数 from cj group by 姓名 union all select 姓名,max(评委五)as 分数 from cj group by 姓名 -- order by 姓名,分数 ) as X group by 姓名 ) group by 姓名 order by 平均分 desc(3)结果 /* 姓名 平均分 张三 98 李四 97 曾六 92 王五 82 */
這樣?select case when count(*)>2 then (sum(result)-(min(result)+max(result)) )/(count(*)-2) else avg(result) end from table1
select id,when count(*)>2 then (sum(分数)-max(分数)-min(分数))/2 else avg(分数) end from tb group by id
create table cj
(姓名 nvarchar(50),
评委一 int,
评委二 int,
评委三 int,
评委四 int,
评委五 int)insert cj
select '张三',100,99,98,97,96 union all
select '李四',99,98,97,96,95 union all
select '王五',80,81,82,83,84 union all
select '曾六',90,91,92,93,94(2)查询
select 姓名,avg(评委一+评委二+评委三+评委四+评委五)/5 as 平均分 from cj
where
评委一 not in
(
select max(分数) from
(
select 姓名,max(评委一)as 分数 from cj group by 姓名 union all
select 姓名,max(评委二)as 分数 from cj group by 姓名 union all
select 姓名,max(评委三)as 分数 from cj group by 姓名 union all
select 姓名,max(评委四)as 分数 from cj group by 姓名 union all
select 姓名,max(评委五)as 分数 from cj group by 姓名
-- order by 姓名,分数
) as X
group by 姓名
)
or
评委二 not in
(
select max(分数) from
(
select 姓名,max(评委一)as 分数 from cj group by 姓名 union all
select 姓名,max(评委二)as 分数 from cj group by 姓名 union all
select 姓名,max(评委三)as 分数 from cj group by 姓名 union all
select 姓名,max(评委四)as 分数 from cj group by 姓名 union all
select 姓名,max(评委五)as 分数 from cj group by 姓名
-- order by 姓名,分数
) as X
group by 姓名
)
or
评委三 not in
(
select max(分数) from
(
select 姓名,max(评委一)as 分数 from cj group by 姓名 union all
select 姓名,max(评委二)as 分数 from cj group by 姓名 union all
select 姓名,max(评委三)as 分数 from cj group by 姓名 union all
select 姓名,max(评委四)as 分数 from cj group by 姓名 union all
select 姓名,max(评委五)as 分数 from cj group by 姓名
-- order by 姓名,分数
) as X
group by 姓名
)
or
评委四 not in
(
select max(分数) from
(
select 姓名,max(评委一)as 分数 from cj group by 姓名 union all
select 姓名,max(评委二)as 分数 from cj group by 姓名 union all
select 姓名,max(评委三)as 分数 from cj group by 姓名 union all
select 姓名,max(评委四)as 分数 from cj group by 姓名 union all
select 姓名,max(评委五)as 分数 from cj group by 姓名
-- order by 姓名,分数
) as X
group by 姓名
)
or
评委五 not in
(
select max(分数) from
(
select 姓名,max(评委一)as 分数 from cj group by 姓名 union all
select 姓名,max(评委二)as 分数 from cj group by 姓名 union all
select 姓名,max(评委三)as 分数 from cj group by 姓名 union all
select 姓名,max(评委四)as 分数 from cj group by 姓名 union all
select 姓名,max(评委五)as 分数 from cj group by 姓名
-- order by 姓名,分数
) as X
group by 姓名
)
group by 姓名
order by 平均分 desc(3)结果
/*
姓名 平均分
张三 98
李四 97
曾六 92
王五 82
*/
case when count(*)>2 then (sum(result)-(min(result)+max(result)) )/(count(*)-2) else avg(result) end
from table1
from tb group by id