SQL
SQL
SELECT
p.product_name,
p.category,
p.subcategory,
p.cost,
s.order_id,
s.sale_date,
s.quantity,
s.price,
s.customer_id
FROM fact_sales s
),
second_step AS (
SELECT
product_name,
category,
subcategory,
cost,
COUNT(order_id) AS order_count,
SUM(quantity) AS quantity_sold,
COUNT(customer_id) AS customer_count,
CAST(SUM(
CASE
ELSE 0
END
) AS REAL) AS lifespan
FROM first_step
),
third_step AS (
SELECT
*,
CASE
ELSE 'Low-Performer'
END AS segment
FROM second_step
SELECT
t.*,
CASE
ELSE 0
END AS avg_order_value,
CASE
ELSE 0
END AS avg_monthly_revenue
FROM third_step t;