如何避免全部合并

时间:2018-10-01 13:02:08

标签: sql oracle union

每个部分的连接条件不同时,任何避免“全部合并”的可能方法

SELECT RS1.*, EXL.*
            FROM "EXL" "EXL"
      INNER JOIN "RS1" "RS1"
              ON "RS1"."HEADER_KEY" = "EXL"."HEADER_KEY"
WHERE "RS1"."PIPE_KEY" = '1109' AND
     "RS1"."COLK" IS NULL  AND
     "RS1".CT1 = 0  AND  "RS1".CT2 > 0

UNION ALL

SELECT RS1.*, EXL.*
            FROM "EXL" "EXL"
      INNER JOIN "RS1" "RS1"
              ON "RS1"."HEADER_KEY" = "EXL"."HEADER_KEY"
      INNER JOIN "YFS"."STATUS_MAP" "SOS"
              ON "SOS"."STATUS" = "RS1"."STATUS"
      INNER JOIN "RS1" "RS2"
              ON "RS2"."LINE_KEY" = "RS1"."CHAINLINE_KEY" AND
                 "RS2"."PIPEKEY" = "SOS"."TYPE_KEY" AND
                 "RS2"."STATUS" = "SOS"."EXTN_STATUS" AND
                 "RS2"."PIPE_KEY" = '4093'
WHERE "RS1"."PIPE_KEY" = '1109' AND
      "RS1"."COLK" IS NULL  AND
      "RS1".CT1 = 0  AND  "RS1".CT2 > 0

1 个答案:

答案 0 :(得分:0)

由于两个SELECT的WHERE条件完全相同,因此我假设即使缺少EXL的行,您也要返回YFS.STATUS_MAP的行(否则请解释一下意图) 。您可以使用LEFT JOIN

SELECT RS1.*, EXL.*
FROM
    "EXL"
    INNER JOIN "RS1"
        ON "RS1"."HEADER_KEY" = "EXL"."HEADER_KEY"
    LEFT JOIN "YFS"."STATUS_MAP" "SOS"
        ON "SOS"."STATUS" = "RS1"."STATUS"
    LEFT JOIN "RS1" "RS2"
        ON "RS2"."LINE_KEY" = "RS1"."CHAIN_LINE_KEY" AND
           "RS2"."PIPEKEY" = "SOS"."TYPE_KEY" AND
           "RS2"."STATUS" = "SOS"."EXTN_STATUS" AND
           "RS2"."PIPELINE_KEY" = '4093'
WHERE "RS1"."PIPELINE_KEY" = '1109' AND
      "RS1"."COLK" IS NULL  AND
      "RS1".CT1 = 0  AND  "RS1".CT2 > 0