将2个查询组合成1个两个查询具有不同的表和求和

时间:2015-09-03 14:39:52

标签: sql oracle

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

 select 
    count(case when sd_code in (select sd_code from sdrp15_submission_log
                                       where QA_date is null) then 1 end) as Changes,
    count(case when sd_code in (select sd_code from sdrp15_submission_log
                                       where QA_date is not null) then 1 end) as Complete
    from sdrp15_cosd

给我结果

   CHANGES   COMPLETE
---------- ----------
       629        210

select a.phase,a.st_code||' - '||b.state_name,
case when a.submission_received_dt is not null then 'Y' else 'N' end as Changes
from pcspro.sdrp15_return a,
pcspro.sdrp15_states_ready b 
where a.phase = b.phase and a.st_code = b.state;

给我结果

PHASE A.ST_CODE||'-'||B.STATE_NAME        CHANGES
----- ----------------------------------- -------
A     01 - Alabama (AL)                   Y      
A     01 - Alabama (AL)                   Y      
A     16 - Idaho (ID)                     Y      
A     30 - Montana (MT)                   Y      
A     56 - Wyoming (WY)                   Y

是否可以创建一个最后出现的行,总计会发生变化并完成?

1 个答案:

答案 0 :(得分:0)

对于你的第一个问题,这样的事情应该有效:

SELECT sum(CASE 
            WHEN sdc.sd_code IN (
                    SELECT ss.sd_code
                    FROM sdrp15_submission_log ss
                    WHERE ss.QA_date IS NULL
                    )
                THEN 1
           ELSE 0
            END) AS Changes
    ,sum(CASE 
            WHEN sdc.sd_code IN (
                    SELECT sdl.sd_code
                    FROM sdrp15_submission_log sdl
                    WHERE sdl.QA_date IS NOT NULL
                    )
                THEN 1
          ELSE 0
            END) AS Complete
FROM sdrp15_cosd sdc