oracle中有如下表:
type     value1    lower     upper
 A         0.11       0.05    0.89
 A         0.23      0.11    0.99
 A         0.81       0.1     0.34 B         0.11       0.75    0.99
 B         0.34       0.05    0.89
 B         0.45      0.01    0.66
 B         0.01      0.05    0.72 C         0.33       0.05    0.79
 C         0.22       0.21    0.79
 C         0.98       0.05    1.00
...我想找出value1 >= lower ,并且value1 <= upper,并且按照type类型进行统计。
结果:  
count  type
   2     A
   2     B
   3     C

解决方案 »

  1.   

    select count(*) count, type from table1 where value1>= lower and value1 <= upper group by type
      

  2.   


    如果是count(*) =0 ,那么这个查询出来的group好像不显示,怎么能显示?
      

  3.   

    select count(*) count, type from table1 where value1>= lower and value1 <= upper group by type
    union 
    select 0,t2.type from table1 t2 where t2. type not in (
    select t3. type  from table1  t3 where t3.value1>= t3.lower and t3.value1 <=t3. upper)
      

  4.   


     ----Type 为'D' 的不符合条件 count显示0
    with t as
     (select 'A' type, 0.11 value1, 0.05 lower, 0.89 upper
        from dual
      union all
      select 'A', 0.23, 0.11, 0.99 upper
        from dual
      union all
      select 'A', 0.81, 0.1, 0.34 upper
        from dual
      union all
      select 'B', 0.11, 0.75, 0.99 upper
        from dual
      union all
      select 'B', 0.34, 0.05, 0.89 upper
        from dual
      union all
      select 'B', 0.45, 0.01, 0.66 upper
        from dual
      union all
      select 'B', 0.01, 0.05, 0.72 upper
        from dual
      union all
      select 'C', 0.33, 0.05, 0.79 upper
        from dual
      union all
      select 'C', 0.22, 0.21, 0.79 upper
        from dual
      union all
      select 'C', 0.98, 0.05, 1.00 upper
        from dual
      union all
      select 'D', 0.98, 0.99, 1.00 upper from dual)
    select nvl(count, 0) count, t2.type
      from (select count(*) count, type
              from t
             where value1 >= lower
               and value1 <= upper
             group by type) t1,
           (select distinct type from t) t2
     where t2.type = t1.type(+)
      

  5.   

    ----Type 为'D' 的不符合条件 count显示0
    with t as
     (select 'A' type, 0.11 value1, 0.05 lower, 0.89 upper
        from dual
      union all
      select 'A', 0.23, 0.11, 0.99 upper
        from dual
      union all
      select 'A', 0.81, 0.1, 0.34 upper
        from dual
      union all
      select 'B', 0.11, 0.75, 0.99 upper
        from dual
      union all
      select 'B', 0.34, 0.05, 0.89 upper
        from dual
      union all
      select 'B', 0.45, 0.01, 0.66 upper
        from dual
      union all
      select 'B', 0.01, 0.05, 0.72 upper
        from dual
      union all
      select 'C', 0.33, 0.05, 0.79 upper
        from dual
      union all
      select 'C', 0.22, 0.21, 0.79 upper
        from dual
      union all
      select 'C', 0.98, 0.05, 1.00 upper
        from dual
      union all
      select 'D', 0.98, 0.99, 1.00 upper from dual)
    select  T.TYPE,SUM(CASE WHEN T.VALUE1>=T.LOWER AND T.VALUE1<=T.UPPER THEN 1 ELSE 0 END ) COUNT from T group by T.TYPE order by T.TYPE