select count(decode(f,0,1))"F=0", count(decode(f,1,1)) "F=1" from t
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
学习下: 这是典型的行列转换问题! 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);
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;
select sum(decode(F,0,1,0)) f=0, sum(decode(F,1,1,0)) f=1 from T
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
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);
SELECT SUM (decode(f,0,1,0)) "f=0", sum(decode(f,1,1,0)) "f=1" FROM t
count(decode(f,1,1)) "F=1"
from t
----------
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
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);
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;
sum(decode(F,1,1,0)) f=1
from T
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
from
(select f,count(*) cnt from table_tab group by f);
sum(decode(f,1,1,0)) "f=1" FROM t