2015-09-14 68 views
0

我无法结合这两个查询,我的问题是我想如何加入这两个查询?我是否使用加入?我想结合这两个查询,因为我正在处理某些要求。我的用例是用于显示sdrp15返回表中是否有日期。关于我的表,连接这两个查询的唯一东西是state_code列和阶段,这两列是我所有表中显示的唯一列。如何结合2个查询?

查询1

select a.phase,a.st_code||' - '||b.state_name AS CHG, 
    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; 

结果1:

PHASE STATE CHG 

A 01 - AL Y   
    A 11 - DC Y  
    A 16 - ID Y  

查询2个

select count(cou_code) as changes, state_code 
    from sdrp15_submission_log sl 
    where state_code in (select distinct state_code from sdrp15_submission_log 
          where state_code = sl.state_code 
          and cou_code != 'All') 
    and qa_date is null 
    and phase = 'A'      
    group by state_code; 

结果2:

CHANGES STATE_CODE 

    -------- ------- 
     29 01   
     2 11   
     2 16   

和我想要做的就是将它们结合起来和我预期的结果应该是:

PHASE STATE CHG CHANGES 

------ ------- ------ -------- 
    A 01 - AL Y  29 
    A 11 - DC Y  02 
    A 16 - ID Y  02 
    A 08 - HA Y  NULL 
+2

你为什么希望将它们组合?你的用例是什么?你能写一下你的桌子吗? –

+0

向我们展示查询1的样本结果和查询2的样本结果,并向我们展示您想要的组合结果! – jarlh

+0

@jarlh更新了帖子 –

回答

1

个人而言,我需要更多的信息。然而,也许下面就为你工作(我没有测试它!):根据评论

SELECT 
    a.phase AS PHASE, 
    a.st_code||' - '||b.state_name AS [STATE], 
    CASE WHEN a.submission_received_dt IS NOT NULL THEN 'Y' ELSE 'N' END AS CHG, 
    x.changes AS CHANGES 
FROM pcspro.sdrp15_return a 
INNER JOIN pcspro.sdrp15_states_ready b 
    ON a.phase = b.phase AND a.st_code = b.state 
LEFT JOIN (
    SELECT 
     COUNT(cou_code) AS changes, 
     state_code 
    FROM sdrp15_submission_log sl 
    WHERE state_code IN (
     SELECT DISTINCT state_code 
     FROM sdrp15_submission_log 
     WHERE state_code = sl.state_code AND cou_code != 'All') 
     AND qa_date IS NULL AND phase = 'A'      
    GROUP BY state_code; 
) x ON x.state_code = a.st_code 

编辑:

SELECT 
    a.phase AS PHASE, 
    a.st_code||' - '||b.state_name AS [STATE], 
    CASE WHEN a.submission_received_dt IS NOT NULL THEN 'Y' ELSE 'N' END AS CHG, 
    x.changes AS CHANGES_qa_date_null, 
    y.changes AS CHANGES_qa_date_not_null 
FROM pcspro.sdrp15_return a 
INNER JOIN pcspro.sdrp15_states_ready b 
    ON a.phase = b.phase AND a.st_code = b.state 
LEFT JOIN (
    SELECT 
     COUNT(cou_code) AS changes, 
     state_code 
    FROM sdrp15_submission_log sl 
    WHERE state_code IN (
     SELECT DISTINCT state_code 
     FROM sdrp15_submission_log 
     WHERE state_code = sl.state_code AND cou_code != 'All') 
     AND qa_date IS NULL AND phase = 'A'      
    GROUP BY state_code; 
) x ON x.state_code = a.st_code 
LEFT JOIN (
    SELECT 
     COUNT(cou_code) AS changes, 
     state_code 
    FROM sdrp15_submission_log sl 
    WHERE state_code IN (
     SELECT DISTINCT state_code 
     FROM sdrp15_submission_log 
     WHERE state_code = sl.state_code AND cou_code != 'All') 
     AND qa_date IS NOT NULL AND phase = 'A'      
    GROUP BY state_code; 
) y ON y.state_code = a.st_code 
+0

有利于你摆脱隐式加入。 – HLGEM

+0

@ e.go。一个问题,可以说我们有第三个查询就像2ndquery(使用表sdrp15)而不是qa_date是null,它不会为空。我可以添加到另一个连接上面的查询吗? –