计算评委打分,评委不是一次打完,能不能写个语句,当评委多于2个时,平均分为(总分-最高分-最低分)/2 如果只有1个或者2个打分就是2个人的avg(result),因为要对成绩进行排序,不想写排序的程序,看能否直接在sql中完成,请教下各位达人,先谢谢了

解决方案 »

  1.   

    你可以参考这个:现在有一个表,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
    */
      

  2.   

    這樣?select 
    case when count(*)>2 then (sum(result)-(min(result)+max(result)) )/(count(*)-2) else avg(result) end
    from table1
      

  3.   

    select id,when count(*)>2 then (sum(分数)-max(分数)-min(分数))/2 else avg(分数) end
    from tb group by id