两个表的两个实例之间的两个连接

时间:2014-08-15 19:06:15

标签: select join nested

我有两个TABLE1实例,名为TABLE A&表C.我还有两个TABLE2实例,称为TABLE B&表D.

我需要将A和B作为GROUP1加入 我需要将C和D作为GROUP2加入

所有在同一个查询中,所以我可以应用WHERE子句来添加条件以查找GROUP1和&之间的匹配。 GROUP2。

不确定最好的方法。两个嵌套的SELECT语句?内部联接?以下是我到目前为止的情况:

SELECT GROUP1.EMPLID,GROUP1.EMPL_STATUS,GROUP1.BANK_CD,GROUP1.ACCOUNT_NUM,GROUP2.EMPLID,GROUP2.EMPL_STATUS,GROUP2.BANK_CD,GROUP2.ACCOUNT_NUM FROM(

(SELECT * FROM           (PS_DIR_DEP_DISTRIB A1,PS_CU_PAF B1            在哪里A1.EMPLID = B1.EMPLID            和B1.EMPL_STATUS IN(' A',' L'' P')            AS GROUP1))

(SELECT * FROM           (PS_DIR_DEP_DISTRIB C1,PS_CU_PAF D1            WHERE C1.EMPLID = D1.EMPLID            和D1.EMPL_STATUS IN(' A',' L',' P')            AS GROUP2))

WHERE GROUP1.EMPLID!= GROUP2.EMPLID   AND GROUP1.EMPL_STATUS IN(' A',' L',' P')   AND GROUP2.EMPL_STATUS IN(' A',' L'' P')   AND GROUP1.BANK_CD = GROUP2.BANK_CD   AND GROUP1.ACCOUNT_NUM = GROUP2.ACCOUNT_NUM )

1 个答案:

答案 0 :(得分:0)

我最终这样做了:

SELECT DISTINCT A.EMPLID "EMP1_ID", B.EMPL_STATUS "EMP1_STATUS", A.BANK_CD "EMP1_BANK", A.ACCOUNT_NUM "EMP1_ACCT", 
            C.EMPLID "EMP2_ID", D.EMPL_STATUS "EMP2_STATUS", C.BANK_CD "EMP2_BANK", C.ACCOUNT_NUM "EMP2_ACCT"

  FROM PS_DIR_DEP_DISTRIB A, PS_CU_PAF B, PS_DIR_DEP_DISTRIB C, PS_CU_PAF D

  WHERE A.EMPLID = B.EMPLID
    AND C.EMPLID = D.EMPLID
    AND A.EMPLID != C.EMPLID
    AND B.JOB_INDICATOR = 'P'
    AND D.JOB_INDICATOR = 'P'
    AND B.EMPL_STATUS IN ('A','L','P')
    AND D.EMPL_STATUS IN ('A','L','P')
    AND A.BANK_CD = C.BANK_CD
    AND A.ACCOUNT_NUM = C.ACCOUNT_NUM
    AND C.ACCOUNT_NUM != ' '

    AND A.EFFDT = (SELECT MAX(A_ED.EFFDT) FROM PS_DIR_DEP_DISTRIB A_ED
                   WHERE A.EMPLID = A_ED.EMPLID)

    AND C.EFFDT = (SELECT MAX(C_ED.EFFDT) FROM PS_DIR_DEP_DISTRIB C_ED
                   WHERE C.EMPLID = C_ED.EMPLID)

ORDER BY 4, 1