0% found this document useful (0 votes)
2 views

SQL

The document outlines a SQL query that analyzes sales data by joining product and sales information. It calculates key metrics such as order count, total revenue, quantity sold, and customer count, and categorizes products into performance segments based on revenue. Additionally, it computes recency, average order value, and average monthly revenue for each product.

Uploaded by

mohamedqadh689
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SQL

The document outlines a SQL query that analyzes sales data by joining product and sales information. It calculates key metrics such as order count, total revenue, quantity sold, and customer count, and categorizes products into performance segments based on revenue. Additionally, it computes recency, average order value, and average monthly revenue for each product.

Uploaded by

mohamedqadh689
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 2

WITH first_step AS (

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

INNER JOIN dim_products p ON s.product_id = p.product_id

),

second_step AS (

SELECT

product_name,

category,

subcategory,

cost,

COUNT(order_id) AS order_count,

SUM(price * quantity) AS total_revenue,

SUM(quantity) AS quantity_sold,

COUNT(customer_id) AS customer_count,

CAST(SUM(

CASE

WHEN strftime('%d', sale_date) = '01' THEN 1

ELSE 0

END
) AS REAL) AS lifespan

FROM first_step

GROUP BY product_name, category, subcategory, cost

),

third_step AS (

SELECT

*,

CASE

WHEN total_revenue >= (SELECT AVG(total_revenue) FROM


second_step) * 1.5 THEN 'High-Performer'

WHEN total_revenue >= (SELECT AVG(total_revenue) FROM


second_step) * 0.75 THEN 'Mid-Range'

ELSE 'Low-Performer'

END AS segment

FROM second_step

SELECT

t.*,

(SELECT CAST(strftime('%J', 'now') - strftime('%J', MAX(f.sale_date)) AS


REAL) / 30 FROM first_step f WHERE f.product_name = t.product_name) AS
recency,

CASE

WHEN order_count > 0 THEN CAST(total_revenue AS REAL) /


order_count

ELSE 0

END AS avg_order_value,

CASE

WHEN lifespan > 0 THEN CAST(total_revenue AS REAL) / lifespan

ELSE 0

END AS avg_monthly_revenue

FROM third_step t;

You might also like