问题简单点:
  如:一个表,分数score表,就两个字段,分数scores,姓名name.
查询的结果要求是:分数段        人数
10-20          XX
20-30          XX
30-80          XX
唉,自己想不出来,感觉很简单,对自己无语了

解决方案 »

  1.   

    case
    类似这样的
    SQL> select * from score;
     
        SCORES NAME
    ---------- ----------
            10 1
            20 1
            21 1
            30 1
            31 1
            40 1
            50 1
     
    7 rows selected
     
    SQL> 
    SQL> select case
      2   when scores >=10 and scores <20
      3   then '[10,20)'
      4   when scores >=20 and scores <30
      5   then '[20,30)'
      6   else
      7   '[30,80)'
      8   end ranges,count(*) from score
      9   group by case
     10   when scores >=10 and scores <20
     11   then '[10,20)'
     12   when scores >=20 and scores <30
     13   then '[20,30)'
     14   else
     15   '[30,80)'
     16   end;
     
    RANGES   COUNT(*)
    ------- ----------
    [10,20)          1
    [20,30)          2
    [30,80)          4
      

  2.   

    要学习oracle了,学习中,呵呵
      

  3.   

    楼主,横向统计是不可能的.
    SQL应该是纵向统计20-30  40-50  60-903       55     22===================
    这种能搞出来的.
      

  4.   

    什么横向纵向的啊横向纵向用sql都能实现,我写的变成行转列就是你的,基本上,sql能实现90%以上有规律的数据统计
      

  5.   

    顶一下
     oracle QQ群号:54775466
              欢迎这方面的爱好者一起探讨。
      

  6.   

    select floor(score/10),count(*)
    from score
    group by floor(score/10);
    供参考。
      

  7.   

      如:一个表,分数score表,就两个字段,分数scores,姓名name.
    select case when scores between 10 and 20 then '10-20'
                when scores between 21 and 30 then '21-30'
                when scores between 31 and 80 then '31-80'
              else '81-100' end, count(*) 人数
    from score
    group by case when scores between 10 and 20 then '10-20'
                when scores between 21 and 30 then '21-30'
                when scores between 31 and 80 then '31-80'
              else '81-100' end
    order by count(*)
      

  8.   

    这种不规格分段应该智能用case语句了,如果是等段统计的话可以试试 
    SELECT DECODE(FLOOR((55-1)/10),5,'51-60') LEV FROM DUAL
      

  9.   

    CREATE TABLE t(
    name varchar2(30), 
    score number(18,2)
    )INSERT INTO t(name,score) values('luoyoumou',77);
    INSERT INTO t(name,score) values('chenli',17);
    INSERT INTO t(name,score) values('liuyang',32);
    INSERT INTO t(name,score) values('huajianguo',44);
    INSERT INTO t(name,score) values('zhangxu',60);
    INSERT INTO t(name,score) values('zhangchunjie',55);
    INSERT INTO t(name,score) values('Tom',89);
    INSERT INTO t(name,score) values('hanchaoyong',23);
    INSERT INTO t(name,score) values('shenxiaofeng',48);
    commit;--------------------------------------------------------------
    SELECT (case when score>=10 and score<20 then '10-20' 
                 when score>=20 and score<30 then '20-30'
                 when score>=30 and score<80 then '30-80'
                 else '' end) as cases, count(1) as "人数"
    from t
    GROUP BY (case when score>=10 and score<20 then '10-20' 
                 when score>=20 and score<30 then '20-30'
                 when score>=30 and score<80 then '30-80'
                 else '' end)
    HAVING (case when score>=10 and score<20 then '10-20' 
                 when score>=20 and score<30 then '20-30'
                 when score>=30 and score<80 then '30-80'
                 else '' end) IS NOT NULL;
    --------------------------------------------------------------
    CASES            人数
    ---------- ----------
    30-80               6
    10-20               1
    20-30               1
      

  10.   

    SELECT (case when score>=10 and score<20 then '10-20' 
                 when score>=20 and score<30 then '20-30'
                 when score>=30 and score<80 then '30-80'
                 else '' end) as cases, count(1) as "人数"
    from t
    WHERE score>=10 and score<80
    GROUP BY (case when score>=10 and score<20 then '10-20' 
                 when score>=20 and score<30 then '20-30'
                 when score>=30 and score<80 then '30-80'
                 else '' end);
      

  11.   

    SELECT (case when score>=10 and score<20 then '10-20' 
                 when score>=20 and score<30 then '20-30'
                 when score>=30 and score<80 then '30-80'
                 else '' end) as "分数段", count(1) as "人数"
    from t
    WHERE score>=10 and score<80
    GROUP BY (case when score>=10 and score<20 then '10-20' 
                 when score>=20 and score<30 then '20-30'
                 when score>=30 and score<80 then '30-80'
                 else '' end);
      

  12.   

    select (select count(*) from t where scores between(90,100)),
           (select count(*) from t where scores between(80,89)),
           ........
     from t