表T,一个字段F,数据如下
0
0
1
1
1希望一个SQL得到当F=0的总数和F=1的总数,得到数据为横向(不要纵向的),结果如下
F=0 F=1
1    3

解决方案 »

  1.   

    select count(decode(f,0,1))"F=0",
      count(decode(f,1,1)) "F=1"
    from t
      

  2.   

    SQL> select * from table_tab;         F
    ----------
             0
             0
             1
             1
             1SQL> select sum(decode(f,0,cnt,0)) "f=0",sum(decode(f,1,cnt,0)) "f=1" from
      2  (select f,count(*) cnt from table_tab group by f);       f=0        f=1
    ---------- ----------
             2          3
      

  3.   

    学习下:   这是典型的行列转换问题!
       select count(decode(f,0,1))"F=0", 
           count(decode(f,1,1)) "F=1" 
       from t
     
      或:
      
       select sum(decode(f,0,cnt,0)) "f=0",sum(decode(f,1,cnt,0)) "f=1" 
       from 
      (select f,count(*) cnt from table_tab group by f); 
      

  4.   


    create table test(
    f number(1));insert into test 
    select 1 from dual union all
    select 1 from dual union all
    select 1 from dual union all
    select 0 from dual union all
    select 0 from dual union all
    select 0 from dual union all
    select 0 from dual union all
    select 0 from dual; select * from test;select count(decode(f,0,1)) "F=0",count(decode(f,1,1)) "F=1" from test t;
      

  5.   

    select sum(decode(F,0,1,0)) f=0,
           sum(decode(F,1,1,0)) f=1
    from T
      

  6.   

    with temp as(
    select 0 f from dual
    union all
    select 0 f from dual
    union all
    select 0 f from dual
    union all
    select 1 f from dual
    union all
    select 1 f from dual
    )
    select count(decode(f,0,1)) "f=0",count(decode(f,1,1)) "f=1" from temp 
      

  7.   

    select sum(decode(f,0,cnt,0)) "f=0",sum(decode(f,1,cnt,0)) "f=1" 
      from 
      (select f,count(*) cnt from table_tab group by f); 
      

  8.   

    SELECT SUM (decode(f,0,1,0))  "f=0",
    sum(decode(f,1,1,0)) "f=1" FROM t