如何组合2个查询?

时间:2015-09-14 12:12:11

标签: sql

我在组合这两个查询时遇到问题,我的问题是我如何加入这两个查询?我使用加入吗?我想结合这两个查询,因为我正在处理某些要求。我的用例是显示sdrp15返回表中是否有日期。关于我的表,链接这两个查询的唯一事情是state_code列和阶段,这两列是我所有表中显示的唯一列。

查询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

1 个答案:

答案 0 :(得分: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