Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- calculate the rolling absolute metrics (not ratios) at each customer age
- DROP TABLE IF EXISTS level24_facts_customer_state.rolling_absolute_metrics_lifetime_cumulative;
- SELECT
- -- ## level24_facts_customer_state.customer_timeline_weekly_extended
- a.customer_id,
- a.customer_country,
- a.is_main_market_customer,
- a.date_week,
- a.life_week_idx,
- a.has_returns_deadline_passed_week,
- a.has_cancellations_deadline_passed_week,
- -- CUMULATIVE
- a.life_week_idx::numeric AS cumulative_life_weeks, -- total weeks in lifecycle up until that point
- CASE WHEN a.life_week_idx >= 14 THEN ROUND(a.life_week_idx::numeric / 13, 2) END AS cumulative_life_quarters,
- CASE WHEN a.life_week_idx >= 53 THEN ROUND(a.life_week_idx::numeric / 52, 2) END AS cumulative_life_years,
- SUM(SUM(CASE WHEN a.order_date_week IS NOT NULL THEN 1 END)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC) AS cumulative_weeks_active, -- total weeks active up until that point
- -- LIFETIME CUMULATIVE
- a.prior_cumulative_order_count,
- SUM(SUM(a.order_count)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_order_count,
- SUM(SUM(a.order_count_complete)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_order_count_complete,
- SUM(SUM(a.units_ordered_count_complete)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_units_ordered_count_complete,
- SUM(SUM(a.units_delivered_count_complete)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_units_delivered_count_complete,
- SUM(SUM(a.units_returned_count_complete)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_units_returned_count_complete,
- SUM(SUM(a.units_cancelled_count_complete)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_units_cancelled_count_complete,
- SUM(SUM(a.order_count_with_markdown_30plus)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_order_count_with_markdown_30plus,
- SUM(SUM(a.order_count_with_promo_voucher_10plus)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_order_count_with_promo_voucher_10plus,
- SUM(SUM(a.order_count_with_good_discount)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_order_count_with_good_discount,
- SUM(SUM(a.units_ordered_count)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_units_ordered_count,
- SUM(SUM(a.units_ordered_count_dresses)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_units_ordered_count_dresses,
- SUM(SUM(a.units_ordered_count_blouses)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_units_ordered_count_blouses,
- SUM(SUM(a.units_ordered_count_tops)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_units_ordered_count_tops,
- SUM(SUM(a.units_ordered_count_jackets)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_units_ordered_count_jackets,
- SUM(SUM(a.units_ordered_count_knitwear)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_units_ordered_count_knitwear,
- SUM(SUM(a.units_ordered_count_trousers)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_units_ordered_count_trousers,
- SUM(SUM(a.units_ordered_count_skirts)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_units_ordered_count_skirts,
- SUM(SUM(a.units_ordered_count_pl1_offered_selling_price)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_units_ordered_count_pl1_offered_selling_price,
- SUM(SUM(a.units_ordered_count_pl2_offered_selling_price)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_units_ordered_count_pl2_offered_selling_price,
- SUM(SUM(a.units_ordered_count_pl3_offered_selling_price)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_units_ordered_count_pl3_offered_selling_price,
- SUM(SUM(a.units_ordered_count_pl4_offered_selling_price)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_units_ordered_count_pl4_offered_selling_price,
- SUM(SUM(a.units_ordered_count_pl5_offered_selling_price)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_units_ordered_count_pl5_offered_selling_price,
- SUM(SUM(a.units_cancelled_count)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_units_cancelled_count,
- SUM(SUM(a.units_returned_count)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_units_returned_count,
- SUM(SUM(a.units_delivered_count)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_units_delivered_count,
- SUM(SUM(a.units_kept_count)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_units_kept_count,
- SUM(SUM(a.value_product_cost_goods_kept)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_product_cost_goods_kept,
- SUM(SUM(a.value_product_cost_goods_ordered)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_product_cost_goods_ordered,
- SUM(SUM(a.value_product_net_margin)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_product_net_margin,
- SUM(SUM(a.value_product_gross_margin)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_product_gross_margin,
- SUM(SUM(a.value_product_sales_excl_vat)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_product_sales_excl_vat,
- SUM(SUM(a.value_product_gross_sales)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_product_gross_sales,
- SUM(SUM(a.value_product_sales_full_price_excl_vat)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_product_sales_full_price_excl_vat,
- SUM(SUM(a.value_product_effective_promotion_voucher_excl_vat)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_product_effective_promotion_voucher_excl_vat,
- SUM(SUM(a.value_product_gross_sales_excl_shipping)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_product_gross_sales_excl_shipping,
- SUM(SUM(a.value_product_gross_sales_pos_excl_vat)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_product_gross_sales_pos_excl_vat,
- SUM(SUM(a.value_product_gross_sales_pos_incl_vat)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_product_gross_sales_pos_incl_vat,
- SUM(SUM(a.value_product_markdown_excl_vat)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_product_markdown_excl_vat,
- SUM(SUM(a.value_product_markdown_incl_vat)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_product_markdown_incl_vat,
- SUM(SUM(a.value_product_net_sales)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_product_net_sales,
- MAX(MAX(a.value_max_product_gross_sales_pos_excl_vat)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_max_product_gross_sales_pos_excl_vat,
- AVG(AVG(a.value_avg_product_gross_sales_pos_excl_vat)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_avg_product_gross_sales_pos_excl_vat,
- AVG(AVG(a.value_avg_product_sales_full_price_excl_vat)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_avg_product_sales_full_price_excl_vat,
- AVG(AVG(a.value_avg_product_selling_price_excl_vat)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_avg_product_selling_price_excl_vat,
- AVG(AVG(a.value_avg_product_selling_price_incl_vat)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_avg_product_selling_price_incl_vat,
- SUM(SUM(a.value_product_total_cancellations_excl_vat)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_product_total_cancellations_excl_vat,
- SUM(SUM(a.value_product_total_credit_incl_vat)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_product_total_credit_incl_vat,
- SUM(SUM(a.value_product_total_returns_excl_vat)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_product_total_returns_excl_vat,
- SUM(SUM(a.value_product_other_direct_cost_without_shipping_excl_vat)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_product_other_direct_cost_without_shipping_excl_vat,
- SUM(SUM(a.value_product_cost_delivery_sent_excl_vat)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_product_cost_delivery_sent_excl_vat,
- SUM(SUM(a.value_product_cost_delivery_returned_excl_vat)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_product_cost_delivery_returned_excl_vat,
- SUM(SUM(a.value_product_cpo_excl_vat)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_product_cpo_excl_vat,
- SUM(SUM(a.value_product_cpnco_max_cpa_excl_vat)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_product_cpnco_max_cpa_excl_vat,
- SUM(SUM(a.value_product_cpnco_acquisition_potential_excl_vat)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_product_cpnco_acquisition_potential_excl_vat,
- SUM(SUM(a.value_product_cprco_retention_potential_excl_vat)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_product_cprco_retention_potential_excl_vat,
- SUM(SUM(a.value_product_cm1)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_product_cm1,
- SUM(SUM(a.value_product_cm2_cpo_model)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_product_cm2_cpo_model,
- SUM(SUM(a.value_product_cm2_max_cpa_model)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_product_cm2_max_cpa_model,
- SUM(SUM(a.value_product_cm2_cpo_acqret_model)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_product_cm2_cpo_acqret_model,
- -- ESTIMATES
- SUM(SUM(a.value_actuals_estimated_product_net_sales)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_estimated_product_net_sales,
- SUM(SUM(a.value_actuals_estimated_product_cm1)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_estimated_product_cm1,
- SUM(SUM(a.value_actuals_estimated_product_cm2_cpo_model)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_estimated_product_cm2_cpo_model,
- SUM(SUM(a.value_actuals_estimated_product_cm2_max_cpa_model)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_estimated_product_cm2_max_cpa_model,
- SUM(SUM(a.value_actuals_estimated_product_cm2_cpo_acqret_model)) OVER (PARTITION BY a.customer_id ORDER BY a.date_week ASC)::numeric AS cumulative_value_estimated_product_cm2_cpo_acqret_model
- INTO
- level24_facts_customer_state.rolling_absolute_metrics_lifetime_cumulative
- FROM
- level24_facts_customer_state.customer_timeline_weekly_extended AS a
- GROUP BY
- a.customer_id,
- a.date_week
- ;
- ALTER TABLE level24_facts_customer_state.rolling_absolute_metrics_lifetime_cumulative ADD PRIMARY KEY (customer_id, date_week);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement