Files
dbt_cloud/models/exchange/intermediate/int_exchange__latest_company_contacts.sql
T
2023-03-28 23:57:11 +08:00

39 lines
786 B
SQL

--IMPORT
WITH contacts AS (
SELECT * FROM {{ ref('stg_exchange__contacts') }}
),
--LOGIC
get_latest_company_contacts AS (
SELECT
*,
row_number() OVER
(PARTITION BY company_id
ORDER BY contact_created_datetime DESC) AS last_row_number
FROM contacts
QUALIFY
last_row_number = 1
),
--FINAL
final__int_exchange__latest_company_contacts AS (
SELECT
id,
company_id,
country_id,
country_name,
reference,
phone,
email,
wechat_id,
contact_deleted_datetime,
contact_created_datetime,
contact_updated_datetime
FROM get_latest_company_contacts
)
SELECT * FROM final__int_exchange__latest_company_contacts