日期:2014-05-17  浏览次数:20879 次

请教一个sql 按一列分成3组,统计出每组的数量的sql怎么写
现在有如下数据:num s_name
  10 xx
  12 a
  13 b
  16 c
  22 d
  33 e
  36 f
我想要得到一个查询结果
  10~15 3
  15~30 2
  30~40 2
请问怎么写sql

------解决方案--------------------
分组有没有规律咯,如果没有规律的话就这样了
首先给基础表做个虚拟字段:
SQL code

select seq,
       count(1) n
  from (
select case when t.num>10 and t.num<=15 then 'a_10_15'
            when t.num>15 and t.num<=30 then 'a_15_30'
            when t.num>30 and t.num<=40 then 'a_30_40' else null end seq; 
  from table t
) group by seq

------解决方案--------------------
探讨
SQL code


select case when num >= 10 and num < 15 then '10~15'
when num >= 15 and num < 30 then '15~30'
when num >= 30 and num < 40 then '30~40'
else null
……

------解决方案--------------------
引用:

SQL code


select case when num > = 10 and num < 15 then '10~15 '
when num > = 15 and num < 30 then '15~30 '
when num > = 30 and num < 40 then '30~40 '
else null
……

2楼的++