diff --git a/models/marts/reporting/rep_exchange__daily_orders.sql b/models/marts/reporting/rep_exchange__daily_orders.sql index 93f63b9..94b977c 100644 --- a/models/marts/reporting/rep_exchange__daily_orders.sql +++ b/models/marts/reporting/rep_exchange__daily_orders.sql @@ -40,6 +40,8 @@ fct_and_dim_joins AS ( companies.has_wallet AS company_has_wallet, companies.latitude AS company_latitude, companies.longitude AS company_longitude, + companies.company_lifetime_value, + companies.m_score_lifetime, orders.user_id, orders.bank_id, @@ -277,6 +279,8 @@ final__rep_exchange__daily_orders AS ( estimate_booking_quote_value, estimate_booking_base_value, estimate_booking_value_rm, + company_lifetime_value, + m_score_lifetime, -- date/times order_created_datetime, @@ -310,4 +314,4 @@ final__rep_exchange__daily_orders AS ( ) -SELECT * FROM final__rep_exchange__daily_orders +SELECT * FROM final__rep_exchange__daily_orders \ No newline at end of file diff --git a/models/marts/warehouse/dim_exchange__companies.sql b/models/marts/warehouse/dim_exchange__companies.sql index 6d0e304..dd0028c 100644 --- a/models/marts/warehouse/dim_exchange__companies.sql +++ b/models/marts/warehouse/dim_exchange__companies.sql @@ -1,3 +1,7 @@ +-- VARIABLES +{% set monetary_list_lifetime = [2000,7000,25000,82000] %} + + -- IMPORTS WITH companies AS ( SELECT * FROM {{ ref('int_exchange__companies_join_segments') }} @@ -27,11 +31,16 @@ currencies AS ( SELECT * FROM {{ ref('stg_exchange__currencies') }} ), --- -- LOGICS +transaction_orders AS ( + SELECT * FROM {{ ref('stg_exchange__transaction_orders') }} +), + + +-- LOGIC count_company_user AS ( SELECT company_id, - COUNT(user_id) AS count_user + COUNT(user_id) AS count_user FROM companies_brg_users @@ -78,13 +87,13 @@ companies_rename_and_join_table AS ( companies.exchange_rate_segment, companies.is_migrated_company, companies.status, - companies.created_datetime AS company_created_datetime, + 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_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, @@ -117,22 +126,21 @@ companies_rename_and_join_table AS ( 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, + 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.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 + count_company_user.count_user AS number_of_user FROM companies @@ -155,6 +163,97 @@ companies_rename_and_join_table AS ( 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.base_value + transaction_orders.base_tax + transaction_orders.base_service_charge ) + 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 @@ -216,6 +315,8 @@ final__dim_exchange__companies AS ( -- measures wallet_balance, number_of_user, + company_lifetime_value, + m_score_lifetime, -- date/times company_created_datetime, @@ -227,7 +328,8 @@ final__dim_exchange__companies AS ( -- metadata _dbt_ran_datetime - FROM companies_rename_and_join_table + FROM lifetime_value + ) SELECT * FROM final__dim_exchange__companies \ No newline at end of file