Shipping data model - company engagement behavior

This commit is contained in:
CIEF ACC1
2023-12-29 09:41:36 +00:00
parent b285a9546d
commit 4514c35db4
@@ -0,0 +1,236 @@
-- AVAILABLE FILTER VALUE
{% set day_use_to_churn = 120 %}
-- IMPORT
WITH companies AS (
SELECT * FROM {{ ref('dim_shipping__companies') }}
),
origin_warehouse_shipping_packing_lists AS (
SELECT * FROM {{ ref('fct_shipping__origin_warehouse_shipping_packing_lists') }}
),
-- LOGIC
remove_supplier_company AS (
SELECT * FROM companies
WHERE
business_type IN ('IMPORTER', 'ONLINE_SELLER')
),
order_date_row_rank AS (
SELECT
*,
ROW_NUMBER() OVER (
PARTITION BY sub_company_id ORDER BY origin_warehouse_shipping_order_created_datetime, origin_warehouse_shipping_packing_list_id
) AS row_rank_index
FROM origin_warehouse_shipping_packing_lists
),
get_orders_of_next_and_previous_order_date AS (
SELECT
order_date_row_rank.*,
companies.sub_company_created_datetime,
companies.sub_company_marking_id,
companies.is_migrated_company,
previous_order_date_row_rank_clone.origin_warehouse_shipping_order_created_datetime AS previous_order_created_datetime
FROM order_date_row_rank
LEFT JOIN companies
ON (order_date_row_rank.sub_company_id = companies.sub_company_id)
LEFT JOIN order_date_row_rank AS next_order_date_row_rank_clone
ON (order_date_row_rank.row_rank_index = next_order_date_row_rank_clone.row_rank_index - 1
AND order_date_row_rank.sub_company_id = next_order_date_row_rank_clone.sub_company_id)
LEFT JOIN order_date_row_rank AS previous_order_date_row_rank_clone
ON (order_date_row_rank.row_rank_index = previous_order_date_row_rank_clone.row_rank_index + 1
AND order_date_row_rank.sub_company_id = previous_order_date_row_rank_clone.sub_company_id)
),
-- Register / Churn / Register; Churn
company_register_events AS (
SELECT
remove_supplier_company.sub_company_id,
remove_supplier_company.sub_company_marking_id,
remove_supplier_company.is_migrated_company,
remove_supplier_company.sub_company_created_datetime AS event_datetime,
'Register' AS register_event,
IFF(
MAX(origin_warehouse_shipping_packing_lists.sub_company_id) IS NULL,
1,
0
) AS is_never_order_company,
IFF(
DATEDIFF(
DAY,
remove_supplier_company.sub_company_created_datetime,
COALESCE(MIN(origin_warehouse_shipping_packing_lists.origin_warehouse_shipping_order_created_datetime), CURRENT_DATE())
) >= {{day_use_to_churn}},
'Churn',
NULL
) AS churn_event,
RTRIM(
CONCAT(
CASE WHEN register_event IS NOT NULL THEN 'Register, ' ELSE '' END,
CASE WHEN churn_event IS NOT NULL THEN 'Churn, ' ELSE '' END
),
', '
) AS concat_event
FROM remove_supplier_company
LEFT JOIN origin_warehouse_shipping_packing_lists
ON (remove_supplier_company.sub_company_id = origin_warehouse_shipping_packing_lists.sub_company_id)
GROUP BY
remove_supplier_company.sub_company_id,
remove_supplier_company.sub_company_marking_id,
remove_supplier_company.is_migrated_company,
remove_supplier_company.sub_company_created_datetime
),
-- FirstTimeOrder / Repeat / Churn / Reactive
first_time_repeat_churn_reactive_order_events AS (
SELECT
sub_company_id,
sub_company_marking_id,
is_migrated_company,
0 AS is_never_order_company,
origin_warehouse_shipping_packing_list_id,
origin_warehouse_shipping_order_status,
origin_warehouse_shipping_order_created_datetime AS event_datetime,
service_type,
total_package_CBM,
IFF(is_first_time_sub_company = 1, 'First Time Order', NULL) AS first_time_order_event,
IFF(is_first_time_sub_company = 0, 'Repeat', NULL) AS repeat_event,
IFF(
DATEDIFF(
DAY,
origin_warehouse_shipping_order_created_datetime,
COALESCE(next_origin_warehouse_shipping_order_created_datetime, CURRENT_DATE())
) >= {{day_use_to_churn}},
'Churn',
NULL
) AS churn_event,
IFF(
DATEDIFF(
DAY,
COALESCE(previous_order_created_datetime, sub_company_created_datetime),
origin_warehouse_shipping_order_created_datetime
) >= {{day_use_to_churn}},
'Reactive',
NULL
) AS reactive_event,
RTRIM(CONCAT(
CASE WHEN first_time_order_event IS NOT NULL THEN 'First Time Order, ' ELSE '' END,
CASE WHEN repeat_event IS NOT NULL THEN 'Repeat, ' ELSE '' END,
CASE WHEN reactive_event IS NOT NULL THEN 'Reactive, ' ELSE '' END,
CASE WHEN churn_event IS NOT NULL THEN 'Churn, ' ELSE '' END
),
', '
) AS concat_event
FROM get_orders_of_next_and_previous_order_date
),
union_event_tables AS (
SELECT
sub_company_id,
sub_company_marking_id,
is_migrated_company,
is_never_order_company,
NULL AS origin_warehouse_shipping_packing_list_id,
NULL AS origin_warehouse_shipping_order_status,
NULL AS service_type,
NULL AS total_package_CBM,
NULL AS first_time_order_event,
NULL AS repeat_event,
NULL AS reactive_event,
register_event,
churn_event,
concat_event,
event_datetime
FROM company_register_events
UNION
SELECT
sub_company_id,
sub_company_marking_id,
is_migrated_company,
is_never_order_company,
origin_warehouse_shipping_packing_list_id,
origin_warehouse_shipping_order_status,
service_type,
total_package_CBM,
first_time_order_event,
repeat_event,
reactive_event,
NULL AS register_event,
churn_event,
concat_event,
event_datetime
FROM first_time_repeat_churn_reactive_order_events
),
-- FINAL
final__rep_shipping__company_engagement_behaviors AS (
SELECT
-- id
sub_company_id,
sub_company_marking_id,
origin_warehouse_shipping_packing_list_id
-- dimension
is_migrated_company,
is_never_order_company,
origin_warehouse_shipping_order_status,
service_type,
first_time_order_event,
repeat_event,
reactive_event,
register_event,
churn_event,
concat_event,
-- measure
total_package_CBM,
-- date/time
event_datetime,
-- metadata
'{{ modules.datetime.datetime.now(modules.pytz.timezone("Asia/Kuala_Lumpur")) }}' AS _dbt_ran_datetime
FROM union_event_tables
)
SELECT * FROM final__rep_shipping__company_engagement_behaviors