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

时间:2018-12-23 21:04:00

标签: oracle oracle11g

我一直在尝试获取给定客户帐号的所有联系方式,但都没有成功(我想获取该客户的主要电子邮件集-如果有的话)

这是我第一次使用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'

1 个答案:

答案 0 :(得分:3)

我建议避免在select子句中使用相关子查询,因为它们通常是查询性能不佳的原因。我还怀疑,当前使用的相关性可能与错误的表owner_table_name = 'HZ_PARTY_SITES'有关,指示该数据应与表apps.hz_party_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'
;

NVL(column,NULL)并没有实现任何目的,如果column已经为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

然后用INNER JOIN替换逗号并将ON放在每一行的末尾

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子句剪切到其所需的位置,并注意每个联接现在都指向一个表ABOVE(因此,我不得不移动一个表)

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
相关问题