2015-09-14 114 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,它不會爲空。我可以添加到另一個連接上面的查詢嗎? –