From 5297dd07562e3b7afe276ed94fdbf8d1a626e195 Mon Sep 17 00:00:00 2001 From: CIEF ACC1 Date: Fri, 12 Jan 2024 01:25:15 +0000 Subject: [PATCH 1/3] build --- .../stg_googlesheet__account_frozen_cases.sql | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 models/staging/googlesheet/stg_googlesheet__account_frozen_cases.sql diff --git a/models/staging/googlesheet/stg_googlesheet__account_frozen_cases.sql b/models/staging/googlesheet/stg_googlesheet__account_frozen_cases.sql new file mode 100644 index 0000000..89787d1 --- /dev/null +++ b/models/staging/googlesheet/stg_googlesheet__account_frozen_cases.sql @@ -0,0 +1,89 @@ +-- IMPORT +WITH account_frozen_cases AS ( + SELECT * FROM {{ source('src_googlesheet_airbyte', 'account_frozen_case') }} +), + + +-- LOGIC +data_casting AS ( + + SELECT + id AS frozen_account_case_id, + _airbyte_raw_id AS frozen_account_cases_airbyte_id, + ticket_id AS crm_ticket_id, + company_marking_id AS recorded_company_marking_id, + booking_marking_id, + ticket_status, + agent_reply_status, + account_freeze_reason, + issue_type, + product_name, + amount_involved AS affected_order_amount, + currency_involved AS affected_order_currency, + investigation_finding, + agent_comment, + manager_comment, + + COALESCE( + TRY_TO_TIMESTAMP(ticket_created_datetime::STRING, 'DD-MM-YYYY'), + TRY_TO_TIMESTAMP(ticket_created_datetime::STRING, 'DD-MM-YYYY HH24:MI:SS') + ) AS ticket_created_datetime, + + COALESCE( + TRY_TO_TIMESTAMP(ticket_updated_datetime::STRING, 'DD-MM-YYYY'), + TRY_TO_TIMESTAMP(ticket_updated_datetime::STRING, 'DD-MM-YYYY HH24:MI:SS') + ) AS ticket_updated_datetime, + + COALESCE( + TRY_TO_TIMESTAMP(ticket_closed_datetime::STRING, 'DD-MM-YYYY'), + TRY_TO_TIMESTAMP(ticket_closed_datetime::STRING, 'DD-MM-YYYY HH24:MI:SS') + ) AS ticket_closed_datetime, + + _airbyte_extracted_at, + + '{{ modules.datetime.datetime.now(modules.pytz.timezone("Asia/Kuala_Lumpur")) }}' AS _dbt_ran_datetime + + FROM account_frozen_cases + +), + + +-- FINAL +final__stg_googlesheet__sales_exchange_lead_activations AS ( + + SELECT + -- ids + frozen_account_cases_airbyte_id, + frozen_account_case_id, + crm_ticket_id, + booking_marking_id, + recorded_company_marking_id, + + -- dimensions + product_name, + ticket_status, + agent_reply_status, + account_freeze_reason, + issue_type, + affected_order_currency, + investigation_finding, + agent_comment, + manager_comment, + + -- measures + affected_order_amount, + + -- date/times + ticket_created_datetime, + ticket_updated_datetime, + ticket_closed_datetime, + + -- metadata + _airbyte_extracted_at + _dbt_ran_datetime + + FROM data_casting + +) + +SELECT * FROM final__stg_googlesheet__sales_exchange_lead_activations \ No newline at end of file From c959a292d61fd2dea37bd3d1e7f4fc52d3c5cf12 Mon Sep 17 00:00:00 2001 From: CIEF ACC1 Date: Fri, 12 Jan 2024 01:25:21 +0000 Subject: [PATCH 2/3] build --- ...fct_exchange__frozen_account_incidents.sql | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 models/marts/warehouse/fct_exchange__frozen_account_incidents.sql diff --git a/models/marts/warehouse/fct_exchange__frozen_account_incidents.sql b/models/marts/warehouse/fct_exchange__frozen_account_incidents.sql new file mode 100644 index 0000000..10e7b5d --- /dev/null +++ b/models/marts/warehouse/fct_exchange__frozen_account_incidents.sql @@ -0,0 +1,92 @@ +-- IMPORT +WITH frozen_account_incidents AS ( + SELECT * FROM {{ ref('stg_googlesheet__account_frozen_cases') }} +), + +bookings AS ( + SELECT * FROM {{ ref('int_exchange__bookings_get_estimate_booking_values') }} +), + +banks AS ( + SELECT * FROM {{ ref('stg_exchange__banks') }} +), + + +-- LOGIC +data_enrich AS ( + + SELECT + frozen_account_incidents.frozen_account_case_id, + frozen_account_incidents.booking_marking_id, + frozen_account_incidents.crm_ticket_id, + frozen_account_incidents.account_freeze_reason, + frozen_account_incidents.ticket_status AS incident_ticket_status, + frozen_account_incidents.agent_reply_status, + frozen_account_incidents.affected_order_amount, + frozen_account_incidents.affected_order_currency, + frozen_account_incidents.ticket_created_datetime, + frozen_account_incidents.ticket_updated_datetime, + frozen_account_incidents.ticket_closed_datetime, + + bookings.booking_id, + bookings.company_id, + bookings.bank_id AS recipient_bank_id, + bookings.service_type, + + banks.country_id AS recipient_bank_country_id, + banks.bank_type AS recipient_bank_type, + banks.bank_name AS recipient_bank_name, + banks.bank_branch AS recipient_bank_branch, + + '{{ modules.datetime.datetime.now(modules.pytz.timezone("Asia/Kuala_Lumpur")) }}' AS _dbt_ran_datetime + + FROM frozen_account_incidents + + LEFT JOIN bookings ON + ( frozen_account_incidents.booking_marking_id = bookings.booking_marking_id ) + + LEFT JOIN banks ON + ( bookings.bank_id = banks.bank_id ) + +), + + +-- FINAL +final__fct_exchange__frozen_account_incidents AS ( + + SELECT + -- ids + frozen_account_case_id, + crm_ticket_id, + booking_id, + booking_marking_id, + company_id, + recipient_bank_id, + recipient_bank_country_id, + + -- dimensions + account_freeze_reason, + incident_ticket_status, + agent_reply_status, + service_type, + recipient_bank_type, + recipient_bank_name, + recipient_bank_branch, + + -- measures + affected_order_amount, + affected_order_currency, + + -- date/times + ticket_created_datetime, + ticket_updated_datetime, + ticket_closed_datetime, + + -- metadata + _dbt_ran_datetime + + FROM data_enrich + +) + +SELECT * FROM final__fct_exchange__frozen_account_incidents \ No newline at end of file From b6b7663bc2d9f8299285ad63a2a177cfe77e3580 Mon Sep 17 00:00:00 2001 From: CIEF ACC1 Date: Fri, 12 Jan 2024 01:25:25 +0000 Subject: [PATCH 3/3] build --- .../rep_exchange__frozen_accounts.sql | 261 ++++++++++++++++++ 1 file changed, 261 insertions(+) create mode 100644 models/marts/reporting/rep_exchange__frozen_accounts.sql diff --git a/models/marts/reporting/rep_exchange__frozen_accounts.sql b/models/marts/reporting/rep_exchange__frozen_accounts.sql new file mode 100644 index 0000000..b73f589 --- /dev/null +++ b/models/marts/reporting/rep_exchange__frozen_accounts.sql @@ -0,0 +1,261 @@ +-- IMPORT +WITH frozen_account_incidents AS ( + SELECT * FROM {{ ref('fct_exchange__frozen_account_incidents') }} +), + +orders AS ( + SELECT * FROM {{ ref('fct_exchange__transaction_orders') }} +), + +costs AS ( + SELECT * FROM {{ ref('fct_exchange__transaction_costs') }} +), + +companies AS ( + SELECT * FROM {{ ref('dim_exchange__companies') }} +), + + +-- LOGIC +affected_companies_order_history AS ( + + SELECT + * + + FROM + orders, + LATERAL + ( + SELECT + frozen_account_case_id, + ticket_created_datetime, + ticket_updated_datetime, + ticket_closed_datetime + FROM + frozen_account_incidents + WHERE + orders.company_id = frozen_account_incidents.company_id + ) + + WHERE transaction_status IN ('UPLOADED_BANK_SLIP') + +), + +order_associated_with_each_incident AS ( +-- Some booking_marking_id may have 2 or more orders +-- This CTE select the order closest to the incident_ticket_created_datetime as the affected order + SELECT + frozen_account_incidents.* EXCLUDE _dbt_ran_datetime, + + orders.order_id, + orders.base_currency_id, + orders.quote_currency_id, + orders.transaction_status, + orders.order_base_value, + orders.order_base_service_charge, + orders.order_base_tax, + orders.order_quote_value, + orders.total_order_value_rm, + orders.order_base_to_quote_currency_exchange_rate, + orders.order_created_datetime, + orders.customer_made_booking_datetime, + orders.customer_placed_order_datetime, + orders.customer_made_payment_datetime, + orders.account_verified_payment_datetime, + orders.first_white_form_generated_datetime, + orders.last_white_form_generated_datetime, + orders.operation_uploaded_bank_slip_datetime, + orders.next_order_created_datetime, + + ABS(DATEDIFF(minute, ticket_created_datetime, operation_uploaded_bank_slip_datetime)) AS time_diff, + + ROW_NUMBER() OVER (PARTITION BY frozen_account_case_id ORDER BY time_diff) AS row_num_idx + + FROM frozen_account_incidents + + LEFT JOIN orders + ON (frozen_account_incidents.booking_marking_id = orders.booking_marking_id) + + QUALIFY + row_num_idx = 1 + +), + +aggregated_metrics AS ( + + SELECT + frozen_account_case_id, + SUM(total_order_value_rm) AS total_lifetime_value_rm, + + SUM(CASE + WHEN + ticket_created_datetime >= customer_placed_order_datetime + AND + transaction_status = 'UPLOADED_BANK_SLIP' + THEN total_order_value_rm + ELSE 0 + END) AS before_frozen_lifetime_value_rm, + + SUM(CASE + WHEN + customer_placed_order_datetime > ticket_created_datetime + AND + transaction_status = 'UPLOADED_BANK_SLIP' + THEN total_order_value_rm + ELSE 0 + END) AS after_unfreeze_lifetime_value_rm, + + MIN(CASE + WHEN + customer_made_booking_datetime > ticket_created_datetime + AND booking_status = 'COMPLETED' + THEN customer_made_booking_datetime + END) AS after_unfreeze_first_booking_datetime, + + MIN(CASE + WHEN customer_placed_order_datetime > ticket_created_datetime + AND order_status = 'COMPLETED' + THEN customer_placed_order_datetime + END) AS after_unfreeze_first_order_datetime, + + SUM(CASE + WHEN ticket_created_datetime > customer_placed_order_datetime + AND transaction_status = 'UPLOADED_BANK_SLIP' + THEN 1 + ELSE 0 + END) AS count_order_before_frozen, + + SUM(CASE + WHEN customer_placed_order_datetime > ticket_created_datetime + AND transaction_status = 'UPLOADED_BANK_SLIP' + THEN 1 + ELSE 0 + END) AS count_order_after_unfreeze, + + MAX(customer_placed_order_datetime) AS latest_order_placed_datetime, + ROUND(DIV0(before_frozen_lifetime_value_rm, count_order_before_frozen), 2) AS before_frozen_average_lifetime_value_rm, + ROUND(DIV0(after_unfreeze_lifetime_value_rm,count_order_after_unfreeze), 2) AS after_unfreeze_average_lifetime_value_rm + + FROM affected_companies_order_history + + GROUP BY frozen_account_case_id + +), + +history_and_metric_join AS ( + + SELECT + order_associated_with_each_incident.*, + + costs.supplier_company_id, + + companies.company_marking_id, + companies.company_type, + companies.business_type, + companies.company_created_datetime AS company_register_datetime, + + aggregated_metrics.total_lifetime_value_rm, + aggregated_metrics.before_frozen_lifetime_value_rm, + aggregated_metrics.after_unfreeze_lifetime_value_rm, + aggregated_metrics.before_frozen_average_lifetime_value_rm, + aggregated_metrics.after_unfreeze_average_lifetime_value_rm, + aggregated_metrics.after_unfreeze_first_booking_datetime, + aggregated_metrics.after_unfreeze_first_order_datetime, + aggregated_metrics.count_order_before_frozen, + aggregated_metrics.count_order_after_unfreeze, + aggregated_metrics.latest_order_placed_datetime, + + COUNT(DISTINCT order_id) + OVER + (PARTITION BY order_associated_with_each_incident.company_id) AS count_company_frozen_cases, + + '{{ modules.datetime.datetime.now(modules.pytz.timezone("Asia/Kuala_Lumpur")) }}' AS _dbt_ran_datetime + + FROM order_associated_with_each_incident + + LEFT JOIN aggregated_metrics + ON (order_associated_with_each_incident.frozen_account_case_id = aggregated_metrics.frozen_account_case_id) + + LEFT JOIN companies + ON (order_associated_with_each_incident.company_id = companies.company_id) + + LEFT JOIN costs + ON (order_associated_with_each_incident.order_id = costs.transaction_order_id) + +), + + +-- FINAL +final_rep_exchange__frozen_accounts AS ( + + SELECT + -- id + frozen_account_case_id, + crm_ticket_id, + booking_id, + booking_marking_id, + company_id, + company_marking_id, + order_id, + base_currency_id, + quote_currency_id, + supplier_company_id, + recipient_bank_id, + recipient_bank_country_id, + + -- dimensions + transaction_status, + account_freeze_reason, + incident_ticket_status, + agent_reply_status, + company_type, + business_type, + service_type, + recipient_bank_type, + recipient_bank_name, + recipient_bank_branch, + affected_order_currency, + + -- measures + count_company_frozen_cases, + affected_order_amount, + order_base_to_quote_currency_exchange_rate, + order_base_value, + order_base_tax, + order_base_service_charge, + order_quote_value, + total_order_value_rm, + total_lifetime_value_rm, + before_frozen_lifetime_value_rm, + after_unfreeze_lifetime_value_rm, + count_order_before_frozen, + count_order_after_unfreeze, + before_frozen_average_lifetime_value_rm, + after_unfreeze_average_lifetime_value_rm, + + -- date/time + company_register_datetime, + ticket_created_datetime, + ticket_updated_datetime, + ticket_closed_datetime, + after_unfreeze_first_booking_datetime, + after_unfreeze_first_order_datetime, + latest_order_placed_datetime, + order_created_datetime, + customer_made_booking_datetime, + customer_placed_order_datetime, + customer_made_payment_datetime, + account_verified_payment_datetime, + first_white_form_generated_datetime, + last_white_form_generated_datetime, + operation_uploaded_bank_slip_datetime, + next_order_created_datetime, + + -- metadata + _dbt_ran_datetime + + FROM history_and_metric_join + +) + +SELECT * FROM final_rep_exchange__frozen_accounts \ No newline at end of file