我的表中有一列成绩,我想加入一列分数段列并对应加入一列在此分数段内的人数,请问怎么实现呢?

解决方案 »

  1.   

    随手敲的,不排除手误select x.*,s,e,v,cnt
       from tb x
    inner join
       (
       select s,e,v,count(*) cnt
          from tb a
       inner join
          (select s=90,e=101,v='优'
           union select 80,90,'良'
           union select 70,80,'中'
           union select 60,70,'级格'
           union select 0,60,'不级格') b
          on a.score>=s and a.score<e
          group by s,e,v
       ) y
       on a.score>=s and a.score<e
      

  2.   

    select id , fs , case when fs < 60 then '不及格'
                          when 60 to 70 then '中'
                          when 70 to 80 then '良'
                          when 80 to 90 then '优'
                          when 90 to 100 then '棒及了'
                          else then '分数有误'
                     end
    from tb
      

  3.   

    select id , fs , case when fs < 60 then '不及格'
                          when fs >= 60 and fs < 70 then '中'
                          when fs >= 70 and fs < 80 then '良'
                          when fs >= 80 and fs < 90 then '优'
                          when fs >= 90 and fs<= 100 then '棒及了'
                          else then '分数有误'
                     end
    from tb