Oracle 正在检索主要联系人的电子邮件地址(查询无法正常工作)

Oracle 正在检索主要联系人的电子邮件地址(查询无法正常工作),oracle,oracle11g,Oracle,Oracle11g,我一直在尝试检索某个客户的所有联系信息,但没有成功,因为我想获取该客户的主要电子邮件集(如果有的话) 这是我第一次使用EBS,有很多表,我想我遗漏了一些东西,因为我的查询工作不正常。它显示了不同的电子邮件,但我认为这是不正确的 你能帮我做这个吗 这是我的疑问: SELECT hp.party_name customer_name,hca.account_number, hca.cust_account_id customer_id, --hcsu.LOCATION customer

我一直在尝试检索某个客户的所有联系信息,但没有成功,因为我想获取该客户的主要电子邮件集(如果有的话)

这是我第一次使用EBS,有很多表,我想我遗漏了一些东西,因为我的查询工作不正常。它显示了不同的电子邮件,但我认为这是不正确的

你能帮我做这个吗

这是我的疑问:

SELECT hp.party_name customer_name,hca.account_number, hca.cust_account_id
       customer_id, --hcsu.LOCATION customer_site_name, 
       hcas.cust_acct_site_id customer_site_id, hl.address1,hl.address2,
       hl.address3,hl.address4,hl.city, hl.province, hl.postal_code,
       NVL ((SELECT DISTINCT phone_number
             FROM hz_contact_points
             WHERE status = 'A'
             AND primary_flag = 'Y'
             AND owner_table_name = 'HZ_PARTY_SITES'
             AND contact_point_type = 'PHONE'
             AND owner_table_id = hcas.cust_acct_site_id
             AND ROWNUM = 1), NULL) phone_number,
       NVL ((SELECT DISTINCT email_address
             FROM hz_contact_points
             WHERE status = 'A'
             AND primary_flag = 'Y'
             AND owner_table_name = 'HZ_PARTY_SITES'
             AND contact_point_type = 'EMAIL'
             AND owner_table_id = hcas.cust_acct_site_id
             AND ROWNUM = 1), NULL) email,
       hcas.status site_status,
       DECODE (hcas.attribute5, 'PUP', 'Y', 'N') usage_type,
       hca.status account_status
FROM apps.hz_cust_accounts hca,
     apps.hz_cust_acct_sites_all hcas,
     apps.hz_parties hp,
     apps.hz_party_sites hps,
     apps.hz_locations hl
WHERE hca.cust_account_id = hcas.cust_account_id
AND hcas.party_site_id = hps.party_site_id
AND hps.location_id = hl.location_id
AND hps.party_id = hp.party_id
AND hcas.status = 'A'
AND hps.status = 'A'
AND hca.status = 'A'
AND hca.account_number='number account'

我建议避免在select子句中使用相关子查询,它们通常是导致查询性能差的一个原因。我还怀疑当前使用的相关性可能与错误的表所有者\u table\u name='HZ\u PARTY\u SITES'表示此数据应与表apps.HZ\u PARTY\u SITES相关,但我无法确定这一点,因为我不知道您的数据

下面我建议使用一个派生表子查询来代替当前使用的相关性

SELECT
    hp.party_name                              customer_name
  , hca.account_number
  , hca.cust_account_id                        
  , customer_id
    --, hcsu.LOCATION customer_site_name
  , hcas.cust_acct_site_id                     customer_site_id
  , hcp.phone_number
  , hcp.email_address
  , hl.address1
  , hl.address2
  , hl.address3
  , hl.address4
  , hl.city
  , hl.province
  , hl.postal_code
  , hcas.status                                site_status
  , DECODE( hcas.attribute5, 'PUP', 'Y', 'N' ) usage_type
  , hca.status                                 account_status
FROM apps.hz_cust_accounts hca
INNER JOIN apps.hz_cust_acct_sites_all hcas ON hca.cust_account_id = hcas.cust_account_id
INNER JOIN apps.hz_party_sites hps ON hcas.party_site_id = hps.party_site_id
INNER JOIN apps.hz_locations hl ON hps.location_id = hl.location_id
INNER JOIN apps.hz_parties hp ON hps.party_id = hp.party_id
LEFT JOIN (
        SELECT
            owner_table_id
          , max(case when contact_point_type = 'PHONE' then phone_number end) phone_number
          , max(case when contact_point_type = 'EMAIL' then email_address end) email_address
        FROM hz_contact_points
        WHERE status = 'A'
        AND primary_flag = 'Y'
        AND owner_table_name = 'HZ_PARTY_SITES'
        AND contact_point_type IN ('EMAIL','PHONE')
        GROUP BY 
            owner_table_id
    ) hcp ON hcas.cust_acct_site_id = hcp.owner_table_id /* not sure of this join */
    AND hcp.rn = 1
WHERE hcas.status = 'A'
AND hps.status = 'A'
AND hca.status = 'A'
AND hca.account_number = 'number account'
;
顺便说一句,NVLcolumn,NULL不能实现任何功能,如果列已经为NULL,那么它将为NULL而不需要NVL

另外,超过25年前,ANSI标准将显式连接语法形式化,这也应该被采用

转换联接时,我总是从在逗号处划分from行开始:

FROM apps.hz_cust_accounts hca
   , apps.hz_cust_acct_sites_all hcas
   , apps.hz_parties hp
   , apps.hz_party_sites hps
   , apps.hz_locations hl
然后将逗号替换为内部联接,并将其放在每一行的末尾

FROM apps.hz_cust_accounts hca
INNER JOIN apps.hz_cust_acct_sites_all hcas ON
INNER JOIN apps.hz_parties hp ON
INNER JOIN apps.hz_party_sites hps ON
INNER JOIN apps.hz_locations hl ON
然后我将连接谓词从where子句剪切到它们所需的位置,注意每个连接现在都引用上面的表,因此我不得不移动一个表

FROM apps.hz_cust_accounts hca
INNER JOIN apps.hz_cust_acct_sites_all hcas ON hca.cust_account_id = hcas.cust_account_id
INNER JOIN apps.hz_party_sites hps ON hcas.party_site_id = hps.party_site_id
INNER JOIN apps.hz_locations hl ON hps.location_id = hl.location_id
/* had to move this down */
INNER JOIN apps.hz_parties hp ON hps.party_id = hp.party_id

我建议避免在select子句中使用相关子查询,它们通常是导致查询性能差的一个原因。我还怀疑当前使用的相关性可能与错误的表所有者\u table\u name='HZ\u PARTY\u SITES'表示此数据应与表apps.HZ\u PARTY\u SITES相关,但我无法确定这一点,因为我不知道您的数据

下面我建议使用一个派生表子查询来代替当前使用的相关性

SELECT
    hp.party_name                              customer_name
  , hca.account_number
  , hca.cust_account_id                        
  , customer_id
    --, hcsu.LOCATION customer_site_name
  , hcas.cust_acct_site_id                     customer_site_id
  , hcp.phone_number
  , hcp.email_address
  , hl.address1
  , hl.address2
  , hl.address3
  , hl.address4
  , hl.city
  , hl.province
  , hl.postal_code
  , hcas.status                                site_status
  , DECODE( hcas.attribute5, 'PUP', 'Y', 'N' ) usage_type
  , hca.status                                 account_status
FROM apps.hz_cust_accounts hca
INNER JOIN apps.hz_cust_acct_sites_all hcas ON hca.cust_account_id = hcas.cust_account_id
INNER JOIN apps.hz_party_sites hps ON hcas.party_site_id = hps.party_site_id
INNER JOIN apps.hz_locations hl ON hps.location_id = hl.location_id
INNER JOIN apps.hz_parties hp ON hps.party_id = hp.party_id
LEFT JOIN (
        SELECT
            owner_table_id
          , max(case when contact_point_type = 'PHONE' then phone_number end) phone_number
          , max(case when contact_point_type = 'EMAIL' then email_address end) email_address
        FROM hz_contact_points
        WHERE status = 'A'
        AND primary_flag = 'Y'
        AND owner_table_name = 'HZ_PARTY_SITES'
        AND contact_point_type IN ('EMAIL','PHONE')
        GROUP BY 
            owner_table_id
    ) hcp ON hcas.cust_acct_site_id = hcp.owner_table_id /* not sure of this join */
    AND hcp.rn = 1
WHERE hcas.status = 'A'
AND hps.status = 'A'
AND hca.status = 'A'
AND hca.account_number = 'number account'
;
顺便说一句,NVLcolumn,NULL不能实现任何功能,如果列已经为NULL,那么它将为NULL而不需要NVL

另外,超过25年前,ANSI标准将显式连接语法形式化,这也应该被采用

转换联接时,我总是从在逗号处划分from行开始:

FROM apps.hz_cust_accounts hca
   , apps.hz_cust_acct_sites_all hcas
   , apps.hz_parties hp
   , apps.hz_party_sites hps
   , apps.hz_locations hl
然后将逗号替换为内部联接,并将其放在每一行的末尾

FROM apps.hz_cust_accounts hca
INNER JOIN apps.hz_cust_acct_sites_all hcas ON
INNER JOIN apps.hz_parties hp ON
INNER JOIN apps.hz_party_sites hps ON
INNER JOIN apps.hz_locations hl ON
然后我将连接谓词从where子句剪切到它们所需的位置,注意每个连接现在都引用上面的表,因此我不得不移动一个表

FROM apps.hz_cust_accounts hca
INNER JOIN apps.hz_cust_acct_sites_all hcas ON hca.cust_account_id = hcas.cust_account_id
INNER JOIN apps.hz_party_sites hps ON hcas.party_site_id = hps.party_site_id
INNER JOIN apps.hz_locations hl ON hps.location_id = hl.location_id
/* had to move this down */
INNER JOIN apps.hz_parties hp ON hps.party_id = hp.party_id
我建议您阅读以下内容:尽管您将问题视为一个查询,但我们只看到查询,而没有关于查询完全依赖的数据的线索。如果没有数据,我们就不知道确切的解决方法是什么。我建议您阅读以下内容:尽管您将问题视为一个查询,但我们只看到该查询,而没有关于该查询完全依赖的数据的线索。没有数据,我们就不知道确切的解决办法是什么。