现有个表 其中有考试成绩,比如
表A姓名   成绩   性别  
张三   80      男
李四   55      男
王五   86      男
赵六   90      男现在想根据分数统计人数 想实现60以下多少人,60-84分多少人,85以上多少人 查询结果如
60以下   1
60-84    1
85以上   2

解决方案 »

  1.   

    select '60以下' as [类型],count(1) as [人数] from [表] where [成绩]<60
    union all
    select '60-84' as [类型],count(1) as [人数] from [表] where [成绩]>=60 and [成绩]<84
    union all
    select '85以上' as [类型],count(1) as [人数] from [表] where [成绩]>=85
      

  2.   


    select 
    count(case when [成绩]<60 then 1 else 0 end) as [60以下],
    count(case when [成绩]>=60 and [成绩]<85 then 1 else 0 end) as [60-84],
    count(case when [成绩]>=85 then 1 else 0 end) as [85以上]
    from [表] 
      

  3.   

    好像只能用union联接查询了吧,case when 方法达不到这样的显示效果。
      

  4.   

    select case when [成绩]<60 then '60以下'
                when [成绩]>=60 and [成绩]<85 then '60-84'
                when [成绩]>=85 then '85以上' end as 类型,
                  count(*) 人数 from tb
    group by case when [成绩]<60 then '60以下'  
                  when [成绩]>=60 and [成绩]<85 then '60-84'
                  when [成绩]>=85 then '85以上' end