Skip to content

Commit 8d6241e

Browse files
Create 5_magnitude_analysis.sql
1 parent 6a412bd commit 8d6241e

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

scripts/5_magnitude_analysis.sql

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
===============================================================================
3+
Magnitude Analysis
4+
===============================================================================
5+
Purpose:
6+
- To quantify data and group results by specific dimensions.
7+
- For understanding data distribution across categories.
8+
9+
SQL Functions Used:
10+
- Aggregate Functions: SUM(), COUNT(), AVG()
11+
- GROUP BY, ORDER BY
12+
===============================================================================
13+
*/
14+
15+
-- Find total customers by countries
16+
SELECT
17+
country,
18+
COUNT(customer_key) AS total_customers
19+
FROM gold.dim_customers
20+
GROUP BY country
21+
ORDER BY total_customers DESC;
22+
23+
-- Find total customers by gender
24+
SELECT
25+
gender,
26+
COUNT(customer_key) AS total_customers
27+
FROM gold.dim_customers
28+
GROUP BY gender
29+
ORDER BY total_customers DESC;
30+
31+
-- Find total products by category
32+
SELECT
33+
category,
34+
COUNT(product_key) AS total_products
35+
FROM gold.dim_products
36+
GROUP BY category
37+
ORDER BY total_products DESC;
38+
39+
-- What is the average costs in each category?
40+
SELECT
41+
category,
42+
AVG(cost) AS avg_cost
43+
FROM gold.dim_products
44+
GROUP BY category
45+
ORDER BY avg_cost DESC;
46+
47+
-- What is the total revenue generated for each category?
48+
SELECT
49+
p.category,
50+
SUM(f.sales_amount) AS total_revenue
51+
FROM gold.fact_sales f
52+
LEFT JOIN gold.dim_products p
53+
ON p.product_key = f.product_key
54+
GROUP BY p.category
55+
ORDER BY total_revenue DESC;
56+
57+
58+
-- What is the total revenue generated by each customer?
59+
SELECT
60+
c.customer_key,
61+
c.first_name,
62+
c.last_name,
63+
SUM(f.sales_amount) AS total_revenue
64+
FROM gold.fact_sales f
65+
LEFT JOIN gold.dim_customers c
66+
ON c.customer_key = f.customer_key
67+
GROUP BY
68+
c.customer_key,
69+
c.first_name,
70+
c.last_name
71+
ORDER BY total_revenue DESC;
72+
73+
-- What is the distribution of sold items across countries?
74+
SELECT
75+
c.country,
76+
SUM(f.quantity) AS total_sold_items
77+
FROM gold.fact_sales f
78+
LEFT JOIN gold.dim_customers c
79+
ON c.customer_key = f.customer_key
80+
GROUP BY c.country
81+
ORDER BY total_sold_items DESC;

0 commit comments

Comments
 (0)