Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2fcb1ab

Browse files
Add files via upload
1 parent ef4be43 commit 2fcb1ab

File tree

6 files changed

+278
-0
lines changed

6 files changed

+278
-0
lines changed

‎SQL_Query-1.sql

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* Analyze the yearly performance of products by comparing their sales
2+
to both the average sales performance of the product and the previous year's sales */
3+
4+
WITH yearly_product_sales as
5+
(
6+
select
7+
YEAR(f.order_date) AS ORDER_YEAR,
8+
p.product_name,
9+
SUM(f.sales_amount) AS CURRENT_SALES
10+
FROM gold.fact_sales f
11+
LEFT JOIN gold.dim_products p
12+
ON f.product_key=p.product_key
13+
WHERE f.order_date is not null
14+
GROUP BY
15+
YEAR(f.order_date),p.product_name
16+
)
17+
SELECT
18+
order_year,
19+
product_name,
20+
CURRENT_SALES,
21+
AVG(current_sales) OVER (PARTITION BY product_name) AS AVG_SALES,
22+
current_sales - AVG(current_sales) OVER (PARTITION BY product_name) AS DIFF_AVG,
23+
CASE WHEN current_sales - AVG(current_sales) OVER (PARTITION BY product_name) > 0 THEN 'Above Avg'
24+
WHEN current_sales - AVG(current_sales) OVER (PARTITION BY product_name) < 0 THEN 'Below Avg'
25+
ELSE 'AVG'
26+
END Avg_change
27+
FROM yearly_product_sales
28+
ORDER BY product_name,order_year

‎SQL_Query-2.sql

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*Segment products into cost ranges and
2+
count how many products fall into each segment */
3+
WITH products_segments AS (
4+
SELECT
5+
product_key,product_name,cost,
6+
CASE WHEN cost < 100 THEN 'Below 100'
7+
WHEN cost BETWEEN 100 AND 500 THEN '100-500'
8+
WHEN cost BETWEEN 500 AND 1000 THEN '500-1000'
9+
ELSE 'Above 1000'
10+
END cost_range
11+
FROM gold.dim_products)
12+
13+
SELECT
14+
cost_range,
15+
COUNT(product_key) AS total_products
16+
FROM products_segments
17+
GROUP BY cost_range
18+
ORDER BY total_products DESC

‎SQL_Query-3.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* Which Categories contribute the most to overall sales?*/
2+
WITH category_sales as (
3+
select p.category,
4+
SUM(s.sales_amount) AS Total_Sales
5+
FROM gold.fact_sales as s
6+
INNER JOIN gold.dim_products as p ON s.product_key=p.product_key
7+
GROUP BY p.category)
8+
9+
SELECT
10+
category,
11+
total_sales,
12+
SUM(total_sales) OVER () overall_sales,
13+
CONCAT(ROUND((CAST (total_sales AS FLOAT) / SUM(total_sales) OVER ())*100,2), '%') as percentage_of_total
14+
FROM category_sales

‎SQL_Query-4.sql

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* Group customers into three segments based on their spending behaviours:
2+
-VIP : Customers with at least 12months of history and spending more than 5000ドル.
3+
-REGULAR : Customers with at least 12 months of history but spending 5000$ or less
4+
-NEW : Customers with a lifespan less than 12 months.
5+
And find the total numbers od customers by each group */
6+
WITH customer_spending AS (
7+
SELECT
8+
c.customer_key,
9+
SUM(f.sales_amount) AS total_spending,
10+
MIN(order_date) first_order,
11+
MAX(order_date) last_order,
12+
DATEDIFF (month,MIN(order_date), MAX(order_date)) AS lifespan
13+
FROM [gold].[fact_sales] f
14+
LEFT JOIN gold.dim_customers c
15+
ON f.customer_key=c.customer_key
16+
GROUP BY c.customer_key
17+
)
18+
19+
SELECT
20+
customer_segment,
21+
COUNT(customer_key) AS total_customers
22+
FROM (
23+
SELECT
24+
customer_key,
25+
CASE WHEN lifespan >= 12 AND total_spending > 5000 THEN 'VIP'
26+
WHEN lifespan >= 12 AND total_spending <= 5000 THEN 'Regular'
27+
ELSE 'New'
28+
END AS customer_segment
29+
FROM customer_spending ) t
30+
GROUP BY customer_segment
31+
ORDER BY total_customers DESC

‎SQL_Query-5.sql

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
CUSTOMER REPORT
3+
4+
Purpose:
5+
- This report consolidates key customer metrics and behaviours.
6+
7+
Highlights:
8+
1. Gathers essential fields such as names,ages,and transaction details.
9+
2. Segments customers into categories (VIP,Regular,New) and age groups.
10+
3.Aggregates customer-level metrics:
11+
-total orders
12+
-total sales
13+
-total quantity purchased
14+
-total products
15+
-lifespan (in months)
16+
4.Calculates valueable KPIs:
17+
- recency(months since last order)
18+
-average order value
19+
-average monthly spend
20+
*/
21+
22+
CREATE VIEW gold.report_customers AS
23+
WITH base_query AS (
24+
/* Base query : Retrieves core olumns from tables */
25+
SELECT
26+
f.order_number,
27+
f.product_key,
28+
f.order_date,
29+
f.sales_amount,
30+
f.quantity,
31+
c.customer_key,
32+
c.customer_number,
33+
CONCAT(c.first_name, '' , c.last_name) AS customer_name,
34+
DATEDIFF(year, c.birthdate, GETDATE()) age
35+
FROM
36+
gold.fact_sales f
37+
LEFT JOIN gold.dim_customers c
38+
ON c.customer_key=f.customer_key
39+
WHERE order_date IS NOT NULL
40+
)
41+
,customer_aggregation AS (
42+
SELECT
43+
customer_key,
44+
customer_number,
45+
customer_name,
46+
age,
47+
COUNT(DISTINCT order_number) AS total_orders,
48+
SUM(sales_amount) AS total_sales,
49+
SUM(quantity) AS total_quantity,
50+
COUNT(DISTINCT product_key) AS total_products,
51+
MAX(order_date) AS last_order_date,
52+
DATEDIFF(month,MIN(order_date), MAX(order_date)) AS lifespan
53+
FROM base_query
54+
GROUP BY
55+
customer_key,
56+
customer_number,
57+
customer_name,
58+
age
59+
)
60+
61+
SELECT
62+
customer_key,
63+
customer_number,
64+
customer_name,
65+
age,
66+
CASE
67+
WHEN age < 20 THEN 'Under 20'
68+
WHEN age between 20 and 29 THEN '20-29'
69+
WHEN age between 30 and 39 THEN '30-39'
70+
WHEN age between 40 and 49 THEN '40-49'
71+
ELSE '50 and above'
72+
END AS age_group,
73+
CASE
74+
WHEN lifespan >= 12 AND total_sales > 5000 THEN 'VIP'
75+
WHEN lifespan >= 12 AND total_sales <= 5000 THEN 'Regular'
76+
ELSE 'New'
77+
END AS customer_segment,
78+
last_order_date,
79+
DATEDIFF(month,last_order_date,GETDATE()) AS resency,
80+
total_orders,
81+
total_sales,
82+
total_quantity,
83+
total_products,
84+
lifespan,
85+
-- Compute average order value( AVO)
86+
CASE WHEN total_sales = 0 THEN 0
87+
ELSE total_sales / total_orders
88+
END AS avg_order_value,
89+
-- Compute average monthly spend
90+
CASE WHEN lifespan = 0 THEN total_sales
91+
ELSE total_sales / lifespan
92+
END AS avg_monthly_spend
93+
FROM customer_aggregation

‎SQL_Query-6.sql

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
PRODUCT REPORT
3+
4+
Purpose:
5+
- This report consolidates key product metrics and behaviours.
6+
7+
Highlights:
8+
1. Gathers essential fields such as product name,category,subcategory and cost.
9+
2. Segments products by revenue to identify High-Performers,Midd-Range,or Low-Range
10+
3.Aggregates product-level metrics:
11+
-total orders
12+
-total sales
13+
-total quantity sold
14+
-total customers (unique)
15+
-lifespan (in months)
16+
4.Calculates valueable KPIs:
17+
- recency(months sincelast sale)
18+
-average order revenue (AOR)
19+
-average monthly revenue
20+
*/
21+
CREATE VIEW gold.report_products AS
22+
WITH base_query AS (
23+
SELECT
24+
s.order_number,
25+
s.order_date,
26+
s.customer_key,
27+
s.sales_amount,
28+
s.quantity,
29+
p.product_key,
30+
p.product_name,
31+
p.category,
32+
p.subcategory,
33+
p.cost
34+
35+
FROM gold.fact_sales s
36+
LEFT JOIN gold.dim_products as p
37+
ON p.product_key=s.product_key
38+
WHERE order_date IS NOT NULL
39+
)
40+
,
41+
product_aggregations AS
42+
(
43+
SELECT
44+
product_key,
45+
product_name,
46+
category,
47+
subcategory,
48+
cost,
49+
DATEDIFF(MONTH,MIN(order_date), MAX(order_date)) AS lifespan,
50+
MAX(order_date) as last_sale_date,
51+
COUNT(DISTINCT order_number) AS total_orders,
52+
COUNT(DISTINCT customer_key) AS total_customers,
53+
SUM(sales_amount) AS total_sales,
54+
SUM(quantity) AS total_quantity,
55+
ROUND(AVG(CAST(sales_amount as FLOAT) / NULLIF(quantity, 0)),1) AS avg_selling_price
56+
FROM base_query
57+
GROUP BY
58+
product_key,
59+
product_name,
60+
category,
61+
subcategory,
62+
cost
63+
)
64+
SELECT
65+
product_key,
66+
product_name,
67+
category,
68+
subcategory,
69+
cost,
70+
last_sale_date,
71+
DATEDIFF(MONTH, last_sale_date,GETDATE()) AS recency_in_months,
72+
CASE
73+
WHEN total_sales > 50000 THEN 'High Performer'
74+
WHEN total_sales >= 10000 THEN 'Mid Range'
75+
ELSE 'Low Performer'
76+
END AS product_segment,
77+
lifespan,
78+
total_orders,
79+
total_customers,
80+
total_sales,
81+
total_quantity,
82+
avg_selling_price,
83+
-- Average ORDER Revenue
84+
CASE
85+
WHEN total_orders = 0 THEN 0
86+
ELSE total_sales / total_orders
87+
END AS avg_order_revenue,
88+
--Average Monthly Revenue
89+
CASE
90+
WHEN lifespan = 0 THEN total_sales
91+
ELSE total_sales / lifespan
92+
END AS avg_monthly_revenue
93+
94+
FROM product_aggregations

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /