两个字段 
name  age
张三   20
李四   50
........求写一条SQL分别统计小于30岁的人数 和 大于30小于60 和大于60岁的人数
要求:
1 一条SQL搞定
2 结果要像下面一样
小于30  |   大于30小于60  |  大于60
----------------------------------
 10人   |   12人           | 8人
SQL怎么写

解决方案 »

  1.   

    select sum(case when age<30 then 1 else 0) 小于30,
    sum(case when age>30  and <60then 1 else 0) 大于30小于60,
    sum(case when age>30 then 1 else 0) 大于60  from tablename
      

  2.   


    /--试一下:select t1.k 小于30, t2.k 大于30小于60, t3.k 大于60
      from (select count(*) k from tableA where age < 30) t1,
           (select count(*) k
              from tableA
             where age >= 30
               and age < 60) t2,
           (select count(*) k from tableA where age >= 60) t3;
      

  3.   

    太快了,几个笔误
    select sum(case when age<30 then 1 else 0) 小于30,
     sum(case when age>30 and age<60 then 1 else 0) 大于30小于60,
     sum(case when age>60 then 1 else 0) 大于60 from tablename
      

  4.   

    这个太EASY了,sum+deocode或者case when都行,一周要回答几次
      

  5.   

    感谢2位给的答案4楼你会当然easy,我不会所以难啊