日期:2014-05-17  浏览次数:20876 次

oracle 查询总记录数问题
有表 order 
ID name status  
----------------------
01 aaa 01
----------------------
02 aba 02
----------------------
03 abc 03
----------------------
04 baa 01
----------------------
05 bac 01

 现要查询 总记录数和 status 为 ‘01’的记录数之差。。。。。

用sql语句怎么做??????

------解决方案--------------------
SQL code

select count(1)-count(case when status = 1 then 1 else null end)
  from order
;

------解决方案--------------------
select count(1) - count(case
when status = '01' then
1
else
0
end)
from order;
------解决方案--------------------
SQL code

with t as
(
select '01' id,'aaa' name,'01' status from dual
union all
select '02' id,'aba' name,'02' status from dual
union all
select '01' id,'aaa' name,'01' status from dual


)
select tab1.a - tab2.b
  from (select count(1) a from t) tab1,
      (select count(1) b from t where t.status = '01') tab2

------解决方案--------------------
SQL code

--查询 总记录数和 status 为 ‘01’ 且 name 是 ‘aaa’,的记录数之差
select count(1)-sum(case when status = '01' and name = 'aaa' then 1 else 0 end)
from order;

-- 查询 总记录数和 status 为 ‘01’ 或者 ‘03’ 的记录数之差
select count(1)-sum(case when status in ('01','03') then 1 else 0 end)
from order;