单行子查询使用子查询返回多行

时间:2014-11-17 21:18:03

标签: sql

我的查询需要一些帮助。以下是我的查询示例。根据消除过程,问题出现在CAL.ACTION周围,基于注释掉行并使用AND ROWNUM< = 1。对此的任何帮助将不胜感激!

SELECT 
    CON.CUSTOMERID||'-'||CON.CONTRACT_NUMBER CONTRACT_NAME,  
    CON.CONTRACT_NUMBER,
    (
        SELECT 
        B.VALUEDATE
        FROM BTV B, VARIABLES V
        WHERE B.VARIABLE_ID = V.VARIABLE_ID
            AND V.NAME = 'ExecutionDate'
            AND B.CONTRACTID = CON.ID
            AND B.REVISION = CON.REVISION
    ) EXECUTION_DATE,
    CON.STATUS CONTRACT_STATUS,
    (
        SELECT 
            IND.FIRST_NAME || ' ' || IND.LAST_NAME
            FROM INDIVIDUAL IND, CONTRACT_ACTIVITY_LOG CAL
            WHERE IND.PARTY_ID = CAL.ORIGINATOR_ID
                AND CAL.ACTION = 'Request Review Task'
                AND CAL.CONTRACT_ID = CON.ID
    ) REVIEW_REQUEST_BY,
    (
        SELECT 
            CC.COMMENTTEXT
        FROM CONTRACTCOMMENT CC, CONTRACT_ACTIVITY_LOG CAL
        WHERE CC.CONTRACTID = CAL.CONTRACT_ID
            AND CC.CONTRACT_COMMENT_ID = CAL.COMMENT_ID
            AND CAL.ACTION = 'Finish Review Task'
            AND CC.CONTRACTID = CON.ID
    ) REVIEWER_COMMENT
FROM CONTRACT CON
WHERE CONTRACT_NUMBER = 138006;

2 个答案:

答案 0 :(得分:0)

要确定违规子查询,请使用COUNT(*) OVER()PARTITION BY您的加入字段,即:

SELECT *,COUNT(*) OVER(PARTITION BY CC.CONTRACTID) As Dup_CT
FROM CONTRACTCOMMENT CC
JOIN CONTRACT_ACTIVITY_LOG CAL
   ON   CC.CONTRACTID = CAL.CONTRACT_ID
    AND CC.CONTRACT_COMMENT_ID = CAL.COMMENT_ID
    AND CAL.ACTION = 'Finish Review Task'
ORDER BY Dup_CT DESC,CC.CONTRACTID

对每个子查询执行相同操作,如果您发现任何Dup_CT值大于1,那么您就会发现问题。

此外,这不会导致您的问题,但我建议不再使用已弃用的语法加入:

SELECT CON.CUSTOMERID||'-'||CON.CONTRACT_NUMBER CONTRACT_NAME
      ,CON.CONTRACT_NUMBER
      ,(SELECT B.VALUEDATE
        FROM BTV B
        JOIN VARIABLES V
          ON    B.VARIABLE_ID = V.VARIABLE_ID
            AND V.NAME = 'ExecutionDate'
        WHERE B.CONTRACTID = CON.ID
          AND B.REVISION = CON.REVISION
        ) EXECUTION_DATE
      ,CON.STATUS CONTRACT_STATUS
      ,(SELECT IND.FIRST_NAME || ' ' || IND.LAST_NAME
        FROM INDIVIDUAL IND
        JOIN CONTRACT_ACTIVITY_LOG CAL
          ON     IND.PARTY_ID = CAL.ORIGINATOR_ID
             AND CAL.ACTION = 'Request Review Task'
        WHERE CAL.CONTRACT_ID = CON.ID
        ) REVIEW_REQUEST_BY
      ,(SELECT CC.COMMENTTEXT
        FROM CONTRACTCOMMENT CC
        JOIN CONTRACT_ACTIVITY_LOG CAL
          ON    CC.CONTRACTID = CAL.CONTRACT_ID
            AND CC.CONTRACT_COMMENT_ID = CAL.COMMENT_ID
            AND CAL.ACTION = 'Finish Review Task'
        WHERE CC.CONTRACTID = CON.ID
        ) REVIEWER_COMMENT
FROM CONTRACT CON
WHERE CONTRACT_NUMBER = 138006;

答案 1 :(得分:0)

提供更多见解。 CAL.ACTION有多种状态。我只关注2'请求审核任务'并且'完成评论Tast'。据说这将使用相同的条件,除了被调用的状态将是不同的。请参阅下面的另一个示例(最终目标是能够查看在“请求审核任务”和“完成审核任务”之间完成CAL.ACTION需要多长时间(天数)'完成审核任务'完成审核任务'完成审核任务&#39 39;:

    SELECT CON.CUSTOMERID||'-'||CON.CONTRACT_NUMBER CONTRACT_NAME
          ,CON.CONTRACT_NUMBER
    ,(SELECT CC.CREATEDON
    FROM ECM8_OWN.CONTRACTCOMMENT CC
    JOIN ECM8_OWN.CONTRACT_ACTIVITY_LOG CAL
    ON CC.CONTRACTID = CAL.CONTRACT_ID
    AND CC.CONTRACT_COMMENT_ID = CAL.COMMENT_ID
    AND CAL.ACTION = 'Request Review Task'
    WHERE CC.CONTRACT_ID = CON.ID) REVIEW_REQUESTED_DATE
    (SELECT CC.CREATEDON
    FROM CONTRACTCOMMENT CC 
    JOIN CONTRACT_ACTIVITY_LOG CAL
    ON CC.CONTRACT_COMMENT_ID = CAL.COMMENT_ID
    AND CC.CONTRACTID = CAL.CONTRACT_ID
    AND CAL.ACTION = 'Finish Review Task'
    WHERE CC.CONTRACT_ID = CON.ID) REVIEW_COMPLETION_DATE
FROM CONTRACT CON
WHERE CONTRACT_NUMBER = 138006;