日期:2014-05-18  浏览次数:20419 次

请教一个做统计用的sql语句
现有个表 其中有考试成绩,比如
表A

姓名 成绩 性别  
张三 80 男
李四 55 男
王五 86 男
赵六 90 男

现在想根据分数统计人数 想实现60以下多少人,60-84分多少人,85以上多少人 查询结果如
60以下 1
60-84 1
85以上 2

------解决方案--------------------
SQL code


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

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

SQL code


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……