需要SQL JOIN语句协助

时间:2018-12-13 16:31:49

标签: sql oracle

我正在创建一个报告,该报告需要同时列出主要人员ID和替代人员ID。但是它还需要同时显示主要人员ID的联系信息和替代人。我现在创建的报告仅列出了主要人员ID的联系信息,但显示了替代ID号。有人可以协助我修复我的SQL,以便列出主要和替代联系方式,而不仅是主要联系方式。我的SQL在下面。

            SELECT "ORG_ACCOUNT".ACCOUNT_NUMBER AS "Account Number",
                   "ORG_PERSON".ADDRESS_2 AS "Address",
                   "ORG_ACCOUNT".DODAAC AS "Dodaac",
                   "ORG_DODAAC".DRA AS "Dra",
                   "ORG_PERSON".EMAIL AS "Email",
                   "ORG_PERSON".FIRST_NAME AS "First Name",
                   "ORG_PERSON".LAST_NAME AS "Last Name",
                   "ORG_PERSON".LAST_TRAIN_DATE AS "Last Train Date",
                   "ORG_PERSON".MIDDLE_NAME AS "Middle Name",
                   "ORG_ALT_ACCOUNT_CUST".PERSON_ID AS "Alt Person Id",
                   "ORG_ORG".ORG_NAME AS "Org Name",
                   "ORG_ACCOUNT".PERSON_ID AS "Person Id",
                   "ORG_PERSON".PHONE_COM AS "Phone Com",
                   "ORG_PERSON".PHONE_DSN AS "Phone Dsn",
                   "ORG_PERSON".RANK AS "Rank"
           FROM "ORG"."ORG_ACCOUNT" "ORG_ACCOUNT",
                "ORG"."ORG_DODAAC" "ORG_DODAAC",
                "ORG"."ORG_ORG" "ORG_ORG",
                "ORG"."ORG_PERSON" "ORG_PERSON"
                "ORG"."ORG_ALT_ACCOUNT_CUST" "ORG_ALT_ACCOUNT_CUST"
           WHERE ( ( "ORG_PERSON".PERSON_ID(+) = ORG_ALT_ACCOUNT_CUST".PERSON_ID )
             AND   ( "ORG_ORG".ORG_ID = "ORG_ACCOUNT".ORG_ID ) 
             AND   ( "ORG_PERSON".PERSON_ID = "ORG_ACCOUNT".PERSON_ID )
             AND   ( "ORG_ALT_ACCOUNT_CUST".PERSON_ID = "ORG_ACCOUNT".PERSON_ID )
             AND   ( "ORG_DODAAC".DODAAC = "ORG_ACCOUNT".DODAAC ) ) 
             AND   ( UPPER("ORG_ACCOUNT".DODAAC) LIKE UPPER(:DODAAC) 
             AND    "ORG_DODAAC".DRA IN ( :P_DRA_ENTRIES)
             AND     UPPER("ORG_ACCOUNT".DODAAC_COMMODITY) = UPPER('A') )
           ORDER BY "ORG_DODAAC".DRA ASC, "ORG_ACCOUNT".ACCOUNT_NUMBER ASC, "ORG_PERSON".LAST_NAME ASC

1 个答案:

答案 0 :(得分:1)

当您要联接一个表两次时,就像在这里使用ORG_PERSON一样,您需要在FROM子句中将其列出两次(使用不同的别名)。

SELECT ORG_ACCOUNT.ACCOUNT_NUMBER AS "Account Number",
                   ORG_PERSON.ADDRESS_2 AS "Address",
                   ORG_ACCOUNT.DODAAC AS "Dodaac",
                   ORG_DODAAC.DRA AS "Dra",
                   ORG_PERSON.EMAIL AS "Email",
                   ORG_PERSON.FIRST_NAME AS "First Name",
                   ORG_PERSON.LAST_NAME AS "Last Name",
                   ORG_PERSON.LAST_TRAIN_DATE AS "Last Train Date",
                   ORG_PERSON.MIDDLE_NAME AS "Middle Name",
                   ORG_ALT_ACCOUNT_CUST.PERSON_ID AS "Alt Person Id",
                   ORG_ORG.ORG_NAME AS "Org Name",
                   ORG_ACCOUNT.PERSON_ID AS "Person Id",
                   ORG_PERSON.PHONE_COM AS "Phone Com",
                   ORG_PERSON.PHONE_DSN AS "Phone Dsn",
                   ORG_PERSON.RANK AS "Rank",
                   alt_person.address_2 as "Alt Address",
                   alt_person.email as "Alt Email",
                   alt_person.first_name as "Alt First Name",
                   alt_person.last_name as "Alt Last Name",
                   alt_person.phone_com as "Alt Phone"
           FROM "ORG".ORG_ACCOUNT ORG_ACCOUNT,
                "ORG".ORG_DODAAC ORG_DODAAC,
                "ORG".ORG_ORG ORG_ORG,
                "ORG".ORG_PERSON ORG_PERSON
                "ORG".ORG_ALT_ACCOUNT_CUST ORG_ALT_ACCOUNT_CUST,
                "ORG".ORG_PERSON alt_person
           WHERE ( ( alt_person.PERSON_ID(+) = ORG_ALT_ACCOUNT_CUST.PERSON_ID )
             AND   ( ORG_ORG.ORG_ID = ORG_ACCOUNT.ORG_ID ) 
             AND   ( ORG_PERSON.PERSON_ID = ORG_ACCOUNT.PERSON_ID )
             AND   ( ORG_ALT_ACCOUNT_CUST.PERSON_ID = ORG_ACCOUNT.PERSON_ID )
             AND   ( ORG_DODAAC.DODAAC = ORG_ACCOUNT.DODAAC ) ) 
             AND   ( UPPER(ORG_ACCOUNT.DODAAC) LIKE UPPER(:DODAAC) 
             AND    ORG_DODAAC.DRA IN ( :P_DRA_ENTRIES)
             AND     UPPER(ORG_ACCOUNT.DODAAC_COMMODITY) = UPPER('A') )
           ORDER BY ORG_DODAAC.DRA ASC, ORG_ACCOUNT.ACCOUNT_NUMBER ASC, ORG_PERSON.LAST_NAME ASC

一些样式说明:我从您的表名和别名中删除了双引号,因为它们令人讨厌且不必要。但是我将查询保留为旧的专有Oracle连接语法而不是ANSI连接,因为我知道许多工作场所仍将其用作内部编码标准。我将更改保留为小写,以便于查看。