Update company_adds increamental bug

This commit is contained in:
Yam ZhengLim
2023-07-17 05:16:28 +00:00
parent 5b982e089a
commit 26dc527126
@@ -0,0 +1,84 @@
-- IMPORTS
WITH company_addresses AS (
SELECT * FROM {{ ref('stg_exchange__company_addresses') }}
),
-- LOGIC
remove_deleted_addresses AS (
SELECT
*
FROM company_addresses
WHERE
deleted_datetime IS NULL
),
lastest_billing_and_non_billing_addresses AS (
SELECT
*,
-- 1 will be latest updated address, rank by updated_at date
row_number() OVER
(PARTITION BY remove_deleted_addresses.company_id, remove_deleted_addresses.is_billing_address
ORDER BY remove_deleted_addresses.updated_datetime DESC) AS lastest_address_rank
FROM remove_deleted_addresses
QUALIFY
lastest_address_rank = 1
),
-- FINAL
final__int_exchange__latest_company_billing_addresses AS (
SELECT
-- ids
company_address_id,
company_id,
country_id,
state_id,
district_id,
google_place_id,
-- dimensions
country_name,
country_short_code,
country_phone_code,
state_name,
district_name,
postcode,
address_line_one,
address_line_two,
is_billing_address,
google_returned_address,
phone_number,
location_type,
operational_status,
latitude,
longitude,
location_website,
location_overall_rating,
location_number_of_reviews,
monday_operating_hours,
tuesday_operating_hours,
wednesday_operating_hours,
thursday_operating_hours,
friday_operating_hours,
saturday_operating_hours,
sunday_operating_hours,
json_place_details_full_return,
-- measures
-- date/times
deleted_datetime,
created_datetime,
updated_datetime,
-- metadata
'{{ modules.datetime.datetime.now(modules.pytz.timezone("Asia/Kuala_Lumpur")) }}' AS _dbt_ran_datetime
FROM lastest_billing_and_non_billing_addresses
)
SELECT * FROM final__int_exchange__latest_company_billing_addresses