diff --git a/dbt/models/marts/core/core_excellent_cohort_revenue.sql b/dbt/models/marts/core/core_excellent_cohort_revenue.sql new file mode 100644 index 0000000..2607b6d --- /dev/null +++ b/dbt/models/marts/core/core_excellent_cohort_revenue.sql @@ -0,0 +1,36 @@ +-- Total revenue summary for products in the Excellent rating cohort (avg_rating >= 4.5) +-- Filters dim_products to rating_tier = 'Excellent' and aggregates revenue, +-- profit, units sold, and margin metrics at the cohort level. +with source as ( + + select + product_id, + product_name, + product_category, + avg_rating, + rating_tier, + total_units_sold, + product_total_revenue, + product_gross_profit, + margin_pct + from {{ ref('dim_products') }} + where rating_tier = 'Excellent' + +), + +final as ( + + select + rating_tier, + count(product_id) as product_count, + sum(product_total_revenue) as total_revenue, + sum(product_gross_profit) as total_gross_profit, + sum(total_units_sold) as total_units_sold, + round(avg(avg_rating)::numeric, 2) as avg_rating, + round(avg(margin_pct)::numeric, 4) as avg_margin_pct + from source + group by rating_tier + +) + +select * from final