Query to obtain latest Note created for a Customer - Account

SELECT
   hca.account_name,
   round(hca.account_number) AS account_number,
   initcap(substr(zn.created_by, 1, instr(zn.created_by, '.') - 1)) || ' ' || initcap(substr(zn.created_by, instr(zn.created_by, '.') + 1)) created_by,
   to_clob(zn.note_txt) AS note_txt,
   initcap(substr(zn.last_updated_by, 1, instr(zn.last_updated_by, '.') - 1)) || ' ' || initcap(substr(zn.last_updated_by, instr(zn.last_updated_by, '.') + 1)) last_updated_by,
   to_char(zn.last_update_date, 'MM/DD/YY') AS created_on
FROM
   hz_cust_accounts hca,
   hz_parties hp,
   zmm_notes zn
WHERE
   hp.party_id = hca.party_id
   AND hca.cust_account_id = zn.source_object_uid
   AND zn.source_object_code = 'CUSTOMER_ACCOUNT'
   AND zn.last_update_date =
   (
      SELECT
         MAX(zn1.last_update_date)
      FROM
         zmm_notes zn1
      WHERE
         zn1.source_object_uid = zn.source_object_uid
   )
ORDER BY
   hp.party_name,
   hca.account_name

Comments