如果没有选择行,count函数应返回0或null

时间:2014-02-12 06:37:57

标签: sql oracle

我写了下面的查询

select process_status_count 
from (select count(*) as process_status_count from prod.process_status) 
where process_status_count <> 18;

要求:如果条件“process_status_count”未满足(process_status_count <> 18为18),我希望process_status_count应返回0。

但是process_status_count没有返回结果(也不是null)。

请建议我需要做哪些更改,如果process_status_count = 18,则结果为零或为空。

4 个答案:

答案 0 :(得分:1)

用例陈述;

select case when process_status_count = 18 then 0 else process_status_count end  from (select count(*) as process_status_count from sidewinder.process_status);

答案 1 :(得分:0)

尝试以下查询,

SELECT CASE process_status_count WHEN 18 THEN process_status_count ELSE 0 END
FROM(
     SELECT COUNT(*) AS process_status_count 
     FROM prod.process_status
     );

答案 2 :(得分:0)

SQL> select count(*), decode(count(*), 18, count(*), 0) fullfill_cond
  2  from user_objects
  3  /

  COUNT(*) FULLFILL_COND                                                        
---------- -------------                                                        
       937             0                                                        

SQL> select count(*), decode(count(*), 937, count(*), 0) fullfill_cond
  2  from user_objects
  3  /

  COUNT(*) FULLFILL_COND                                                        
---------- -------------                                                        
       937           937 

答案 3 :(得分:0)

希望这会对你有所帮助:

with process_status
as
(
select 'Y' as process_status_count from dual
connect by level <=18
)
select decode(count(process_status_count),18,0,count(process_status_count)) as status   
from   process_status