如何将这两个sql查询合二为一?

时间:2015-09-01 15:51:50

标签: sql oracle

select count(*) from sdrp15_cosd where sd_code 
in (select sd_code from sdrp15_submission_log 
where QA_DATE IS null);

select count(*) from sdrp15_cosd where sd_code 
in (select sd_code from sdrp15_submission_log 
where QA_DATE IS not null);

如何在SQL中将这两个查询合并为一个。

3 个答案:

答案 0 :(得分:1)

使用union all

select count(*) as count
from sdrp15_cosd 
where sd_code in (select sd_code from sdrp15_submission_log where QA_DATE IS null)
union all
select count(*) 
from sdrp15_cosd 
where sd_code in (select sd_code from sdrp15_submission_log where QA_DATE IS not null)

编辑:

select 
case when sd_code in 
(select sd_code from sdrp15_submission_log where QA_DATE IS null) then count(*) end 
as count_null,
case when sd_code in
(select sd_code from sdrp15_submission_log where QA_DATE IS not null) then count(*) end 
as count_not_null
from sdrp15_cosd 

答案 1 :(得分:1)

假设sdrp15_submission_log.sd_code没有重复,您可以使用一个查询和条件聚合轻松完成此操作:

select count(*) - count(l.QA_DATE), count(l.QA_DATE)
from sdrp15_cosd c join
     sdrp15_submission_log l
     on c.sd_code = l.sd_code ;

答案 2 :(得分:0)

如果您只是想要QA_DATE的两个行计数为空且QA_DATE IS为空,那么我不明白为什么你不能这样做

select 
    (select count(*) from sdrp15_cosd where sd_code in (select sd_code from
     sdrp15_submission_log where QA_DATE IS null)) as QA_DATE_NULL
    ,(select count(*) from sdrp15_cosd where sd_code in (select sd_code from 
    sdrp15_submission_log where QA_DATE IS not null)) as QA_DATE_NOTNULL