select count(*) from tb1 where (a<9 and a >1) or (a<50 and a >40) or (a<120 and a >100);

解决方案 »

  1.   

    select count(*) from tb1 where (a<9 and a >1) or (a<50 and a >40) or (a<120 and a >100);
    ---------------------------------------------------------------
    这样写好象把他都统计出总和了??
    应该这样吧,2种方法
    方法1
    select count(*)
     from test1
    group by (
    case  
    when to_number(tb1)>1 and  to_number(tb1)<9 then 1  
    when to_number(tb1)>40 and  to_number(tb1)<50 then 2  
    when to_number(tb1)>100 and  to_number(tb1)<120 then 2  
    end)
    方法2
    这是把他统计到一行的
    select sum(case  
    when to_number(tb1)>1 and  to_number(tb1)<9 then 1  else 0 end ),
     sum(case  
    when to_number(tb1)>40 and  to_number(tb1)<50 then 1 else 0 end ),
     sum(case  
    when to_number(tb1)>100 and  to_number(tb1)<120 then 1  else 0 end )
     from test1
      

  2.   

    方法1
    select count(*)
     from test1
    group by (
    case  
    when to_number(tb1)>1 and  to_number(tb1)<9 then 1  
    when to_number(tb1)>40 and  to_number(tb1)<50 then 2  
    when to_number(tb1)>100 and  to_number(tb1)<120 then 3  
    end)
    写错了,应该是3,我写成了2,最后一个then
      

  3.   

    嗯,用or就统计出当a值处在这三个范围中的记录数