日期:2014-05-17 浏览次数:20955 次
SELECT '0-9' "分数", SUM((CASE WHEN 分数>=0 AND 分数<=9 THEN 1 ELSE 0 END))"人数" FROM t_name UNION SELECT '10-19' "分数", SUM((CASE WHEN 分数>=10 AND 分数<=19 THEN 1 ELSE 0 END))"人数" FROM t_name UNION SELECT '20-29' "分数", SUM((CASE WHEN 分数>=20 AND 分数<=29 THEN 1 ELSE 0 END))"人数" FROM t_name UNION SELECT '30-39' "分数", SUM((CASE WHEN 分数>=30 AND 分数<=39 THEN 1 ELSE 0 END))"人数" FROM t_name UNION SELECT '40-49' "分数", SUM((CASE WHEN 分数>=40 AND 分数<=49 THEN 1 ELSE 0 END))"人数" FROM t_name UNION SELECT '50-59' "分数", SUM((CASE WHEN 分数>=50 AND 分数<=59 THEN 1 ELSE 0 END))"人数" FROM t_name UNION SELECT '60-69' "分数", SUM((CASE WHEN 分数>=60 AND 分数<=69 THEN 1 ELSE 0 END))"人数" FROM t_name UNION SELECT '70-79' "分数", SUM((CASE WHEN 分数>=70 AND 分数<=79 THEN 1 ELSE 0 END))"人数" FROM t_name UNION SELECT '80-89' "分数", SUM((CASE WHEN 分数>=80 AND 分数<=89 THEN 1 ELSE 0 END))"人数" FROM t_name UNION SELECT '90-99' "分数", SUM((CASE WHEN 分数>=90 AND 分数<=99 THEN 1 ELSE 0 END))"人数" FROM t_name UNION SELECT '100' "分数", SUM((CASE WHEN 分数=100 THEN 1 ELSE 0 END))"人数" FROM t_name
------解决方案--------------------
select '...到...' 分数,sum(case when 分数>... and 分数<... then 1 else 0 end) 人数 from tb union all '...到...' 分数,sum(case when 分数>... and 分数<... then 1 else 0 end) 人数 from tb union all ... union all '...到...' 分数,sum(case when 分数>... and 分数<... then 1 else 0 end) 人数 from tb