Files
dbt_cloud/models/marts/warehouse/dim_exchange__companies.sql
T
2024-03-07 02:05:22 +00:00

346 lines
12 KiB
SQL

-- VARIABLES
{% set monetary_list_lifetime = [2000,7000,25000,82000] %}
-- 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') }}
),
transaction_orders AS (
SELECT * FROM {{ ref('stg_exchange__transaction_orders') }}
),
-- LOGIC
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 IN ('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
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)
),
lifetime_value AS (
SELECT
companies_rename_and_join_table.*,
ROUND(
ZEROIFNULL(SUM(
CASE
WHEN transaction_orders.status IN ('COMPLETED', 'APPROVED')
THEN transaction_orders.total_base_value
END
))
,2
) AS company_lifetime_value,
CASE
WHEN company_lifetime_value <= {{monetary_list_lifetime[0]}} THEN 1
WHEN (company_lifetime_value > {{monetary_list_lifetime[0]}} AND company_lifetime_value <= {{monetary_list_lifetime[1]}}) THEN 2
WHEN (company_lifetime_value > {{monetary_list_lifetime[1]}} AND company_lifetime_value <= {{monetary_list_lifetime[2]}}) THEN 3
WHEN (company_lifetime_value > {{monetary_list_lifetime[2]}} AND company_lifetime_value <= {{monetary_list_lifetime[3]}}) THEN 4
WHEN company_lifetime_value > {{monetary_list_lifetime[3]}} THEN 5
ELSE -999
END AS m_score_lifetime,
'{{ modules.datetime.datetime.now(modules.pytz.timezone("Asia/Kuala_Lumpur")) }}' AS _dbt_ran_datetime
FROM companies_rename_and_join_table
LEFT JOIN transaction_orders
ON (companies_rename_and_join_table.company_id = transaction_orders.company_id)
GROUP BY
companies_rename_and_join_table.company_id,
companies_rename_and_join_table.company_marking_id,
companies_rename_and_join_table.autocount_id,
companies_rename_and_join_table.contact_id,
companies_rename_and_join_table.company_address_id,
companies_rename_and_join_table.country_id,
companies_rename_and_join_table.state_id,
companies_rename_and_join_table.district_id,
companies_rename_and_join_table.wallet_id,
companies_rename_and_join_table.wallet_currency_id,
companies_rename_and_join_table.identity_document_id,
companies_rename_and_join_table.google_place_id,
companies_rename_and_join_table.name,
companies_rename_and_join_table.company_type,
companies_rename_and_join_table.business_type,
companies_rename_and_join_table.exchange_rate_segment,
companies_rename_and_join_table.is_migrated_company,
companies_rename_and_join_table.status,
companies_rename_and_join_table.contact_name,
companies_rename_and_join_table.contact_phone,
companies_rename_and_join_table.contact_email,
companies_rename_and_join_table.contact_wechat,
companies_rename_and_join_table.country_name,
companies_rename_and_join_table.country_short_code,
companies_rename_and_join_table.state_name,
companies_rename_and_join_table.district_name,
companies_rename_and_join_table.postcode,
companies_rename_and_join_table.address_line_one,
companies_rename_and_join_table.address_line_two,
companies_rename_and_join_table.has_wallet,
companies_rename_and_join_table.wallet_currency_name,
companies_rename_and_join_table.identity_document_type,
companies_rename_and_join_table.identity_reference,
companies_rename_and_join_table.identity_status,
companies_rename_and_join_table.google_returned_address,
companies_rename_and_join_table.google_api_extracted_phone_number,
companies_rename_and_join_table.location_type,
companies_rename_and_join_table.operational_status,
companies_rename_and_join_table.latitude,
companies_rename_and_join_table.longitude,
companies_rename_and_join_table.location_website,
companies_rename_and_join_table.location_overall_rating,
companies_rename_and_join_table.location_number_of_reviews,
companies_rename_and_join_table.monday_operating_hours,
companies_rename_and_join_table.tuesday_operating_hours,
companies_rename_and_join_table.wednesday_operating_hours,
companies_rename_and_join_table.thursday_operating_hours,
companies_rename_and_join_table.friday_operating_hours,
companies_rename_and_join_table.saturday_operating_hours,
companies_rename_and_join_table.sunday_operating_hours,
companies_rename_and_join_table.json_place_details_full_return,
companies_rename_and_join_table.wallet_balance,
companies_rename_and_join_table.number_of_user,
companies_rename_and_join_table.company_created_datetime,
companies_rename_and_join_table.wallet_created_datetime,
companies_rename_and_join_table.wallet_updated_datetime,
companies_rename_and_join_table.identity_approved_datetime,
companies_rename_and_join_table.identity_created_datetime
),
-- 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,
company_lifetime_value,
m_score_lifetime,
-- date/times
company_created_datetime,
wallet_created_datetime,
wallet_updated_datetime,
identity_approved_datetime,
identity_created_datetime,
-- metadata
_dbt_ran_datetime
FROM lifetime_value
)
SELECT * FROM final__dim_exchange__companies