Files
dbt_cloud/models/marts/warehouse/dim_exchange__companies.sql
T
2023-07-17 05:17:50 +00:00

233 lines
7.3 KiB
SQL

-- IMPORTS
WITH companies AS (
SELECT * FROM {{ ref('int_exchange__companies_join_segments') }}
),
company_addresses AS (
SELECT * FROM {{ ref('int_exchange__latest_company_billing_addresses') }}
),
company_wallets AS (
SELECT * FROM {{ ref('stg_exchange__company_wallets') }}
),
companies_brg_users AS (
SELECT * FROM {{ ref('stg_exchange__companies_brg_users') }}
),
company_contacts AS (
SELECT * FROM {{ ref('stg_exchange__company_contacts') }}
),
company_documents AS (
SELECT * FROM {{ ref('stg_exchange__company_documents') }}
),
currencies AS (
SELECT * FROM {{ ref('stg_exchange__currencies') }}
),
-- -- LOGICS
count_company_user AS (
SELECT
company_id,
COUNT(user_id) AS count_user
FROM companies_brg_users
GROUP BY company_id
),
company_billing_addresses AS (
SELECT
*
FROM company_addresses
WHERE
is_billing_address = '1'
),
latest_company_upload_identity_documents AS (
SELECT
*,
row_number() OVER
(PARTITION BY company_id
ORDER BY created_datetime DESC) AS latest_row_number
FROM company_documents
WHERE
document_type IN ('IDENTITY_CARD','SSM_REGISTRATION')
AND
status = 'APPROVED'
QUALIFY
latest_row_number = '1'
),
companies_rename_and_join_table AS (
SELECT
companies.company_id,
companies.name,
companies.company_marking_id,
companies.autocount_id,
companies.company_type,
companies.business_type,
companies.exchange_rate_segment,
companies.is_migrated_company,
companies.status,
companies.created_datetime AS company_created_datetime,
company_contacts.contact_id,
company_contacts.contact_name,
company_contacts.phone AS contact_phone,
company_contacts.email AS contact_email,
company_contacts.wechat AS contact_wechat,
company_billing_addresses.company_address_id,
company_billing_addresses.country_id,
company_billing_addresses.country_name,
company_billing_addresses.country_short_code,
company_billing_addresses.state_id,
company_billing_addresses.state_name,
company_billing_addresses.district_id,
company_billing_addresses.district_name,
company_billing_addresses.postcode,
company_billing_addresses.address_line_one,
company_billing_addresses.address_line_two,
company_billing_addresses.google_place_id,
company_billing_addresses.phone_number AS google_api_extracted_phone_number,
company_billing_addresses.google_returned_address,
company_billing_addresses.location_type,
company_billing_addresses.operational_status,
company_billing_addresses.latitude,
company_billing_addresses.longitude,
company_billing_addresses.location_website,
company_billing_addresses.location_overall_rating,
company_billing_addresses.location_number_of_reviews,
company_billing_addresses.monday_operating_hours,
company_billing_addresses.tuesday_operating_hours,
company_billing_addresses.wednesday_operating_hours,
company_billing_addresses.thursday_operating_hours,
company_billing_addresses.friday_operating_hours,
company_billing_addresses.saturday_operating_hours,
company_billing_addresses.sunday_operating_hours,
company_billing_addresses.json_place_details_full_return,
company_wallets.wallet_id,
IFF(company_wallets.wallet_id IS NOT NULL, 1, 0) AS has_wallet,
company_wallets.amount AS wallet_balance,
company_wallets.currency_id AS wallet_currency_id,
currencies.name AS wallet_currency_name,
company_wallets.created_datetime AS wallet_created_datetime,
company_wallets.updated_datetime AS wallet_updated_datetime,
latest_company_upload_identity_documents.document_id AS identity_document_id,
latest_company_upload_identity_documents.document_type AS identity_document_type,
latest_company_upload_identity_documents.reference AS identity_reference,
latest_company_upload_identity_documents.status AS identity_status,
latest_company_upload_identity_documents.approved_datetime AS identity_approved_datetime,
latest_company_upload_identity_documents.created_datetime AS identity_created_datetime,
count_company_user.count_user AS number_of_user,
'{{ modules.datetime.datetime.now(modules.pytz.timezone("Asia/Kuala_Lumpur")) }}' AS _dbt_ran_datetime
FROM companies
LEFT JOIN company_billing_addresses
ON (companies.company_id = company_billing_addresses.company_id)
LEFT JOIN company_wallets
ON (companies.company_id = company_wallets.company_id)
LEFT JOIN count_company_user
ON (companies.company_id = count_company_user.company_id)
LEFT JOIN company_contacts
ON (companies.company_id = company_contacts.company_id)
LEFT JOIN latest_company_upload_identity_documents
ON (companies.company_id = latest_company_upload_identity_documents.company_id)
LEFT JOIN currencies
ON (company_wallets.currency_id = currencies.currency_id)
),
-- FINAL
final__dim_exchange__companies AS (
SELECT
-- ids
company_id,
company_marking_id,
autocount_id,
contact_id,
company_address_id,
country_id,
state_id,
district_id,
wallet_id,
wallet_currency_id,
identity_document_id,
google_place_id,
-- dimensions
name,
company_type,
business_type,
exchange_rate_segment,
is_migrated_company,
status,
contact_name,
contact_phone,
contact_email,
contact_wechat,
country_name,
country_short_code,
state_name,
district_name,
postcode,
address_line_one,
address_line_two,
has_wallet,
wallet_currency_name,
identity_document_type,
identity_reference,
identity_status,
google_returned_address,
google_api_extracted_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
wallet_balance,
number_of_user,
-- date/times
company_created_datetime,
wallet_created_datetime,
wallet_updated_datetime,
identity_approved_datetime,
identity_created_datetime,
-- metadata
_dbt_ran_datetime
FROM companies_rename_and_join_table
)
SELECT * FROM final__dim_exchange__companies