diff --git a/models/intermediate/int_exchange__latest_company_billing_addresses.sql b/models/intermediate/int_exchange__latest_company_billing_addresses.sql new file mode 100644 index 0000000..e94daff --- /dev/null +++ b/models/intermediate/int_exchange__latest_company_billing_addresses.sql @@ -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 \ No newline at end of file