Query to obtain Customer - Contact information

SELECT
   hp.party_name,
   hca.account_name,
   hps.party_site_name AS site_name,
   hcasa.orig_system_reference AS site_number,
   hl.address1,
   hl.address2,
   hl.city,
   nvl(hl.state, hl.province) AS state,
   hl.postal_code,
   custc.person_first_name AS contact_first_name,
   custc.person_last_name AS contact_last_name,
   custc.email_address,
   custc.primary_phone_area_code,
   custc.primary_phone_number
FROM
   hz_cust_acct_sites_all hcasa,
   hz_cust_accounts hca,
   hz_parties hp,
   hz_party_sites hps,
   hz_locations hl,
   (
      SELECT
         hp1.person_first_name person_first_name,
         hp1.person_last_name person_last_name,
         hp1.primary_phone_area_code primary_phone_area_code,
         hp1.email_address email_address,
         hp1.primary_phone_number primary_phone_number,
         hr.relationship_id relationship_id,
         hcar.cust_account_id cust_account_id,
         hcar.cust_acct_site_id cust_acct_site_id
      FROM
         hz_relationships hr,
         hz_parties hp1,
         hz_cust_account_roles hcar
      WHERE
         hr.relationship_code = 'CONTACT_OF'
         AND hr.subject_id = hp1.party_id
         AND hcar.relationship_id = hr.relationship_id
   )
   custc
WHERE
   1 = 1
   AND hcasa.cust_acct_site_id = custc.cust_acct_site_id( + )
   AND hcasa.cust_account_id = hca.cust_account_id
   AND hp.party_id = hca.party_id
   AND hp.party_id = hps.party_id
   AND hps.party_site_id = hcasa.party_site_id
   AND hps.location_id = hl.location_id
ORDER BY
   hp.party_name,
   hca.account_name,
   hcasa.orig_system_reference

Comments