-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTeam3_Sql_Script.sql
More file actions
202 lines (165 loc) · 4.22 KB
/
Copy pathTeam3_Sql_Script.sql
File metadata and controls
202 lines (165 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
-- Create a cleaned version of Transactions table
CREATE TABLE clean_transactions AS
SELECT DISTINCT
Invoice,
LTRIM(RTRIM(StockCode)) AS StockCode,
Description,
Quantity,
InvoiceDate,
Price,
CustomerID,
Country,
ISNULL(Channel, 'Unknown') AS Channel,
PaymentMethod,
(Quantity * Price) AS Revenue
INTO clean_transactions
FROM Transactions
WHERE
Price > 0
AND Quantity > 0
AND CustomerID IS NOT NULL;
--Join Products sheet to get category
SELECT
t.*,
p.ProductName,
p.Category
INTO final_transactions
FROM clean_transactions t
LEFT JOIN Products p
ON LTRIM(RTRIM(t.StockCode)) = LTRIM(RTRIM(p.StockCode));
SELECT TOP 10 * FROM final_transactions;
SELECT COUNT(*) FROM final_transactions;
SELECT COUNT(*) FROM clean_transactions;
--Revenue by Country & Month
SELECT
Country,
FORMAT(InvoiceDate, 'yyyy-MM') AS Month,
SUM(Revenue) AS Total_Revenue
FROM final_transactions
GROUP BY
Country,
FORMAT(InvoiceDate, 'yyyy-MM')
ORDER BY
Month,
Total_Revenue DESC;
--Top 10 Customers by Total Spend
SELECT TOP 10
CustomerID,
SUM(Revenue) AS Total_Spend
FROM final_transactions
GROUP BY CustomerID
ORDER BY Total_Spend DESC;
--Monthly Active Customers (MAC)
SELECT
FORMAT(InvoiceDate, 'yyyy-MM') AS Month,
COUNT(DISTINCT CustomerID) AS Active_Customers
FROM final_transactions
GROUP BY FORMAT(InvoiceDate, 'yyyy-MM')
ORDER BY Month;
--Products with Highest Cancellation Rate
SELECT TOP 10
t.StockCode,
p.ProductName,
p.Category,
COUNT(*) AS Total_Orders,
SUM(CASE WHEN t.Quantity < 0 THEN 1 ELSE 0 END) AS Cancelled_Orders,
CAST(SUM(CASE WHEN t.Quantity < 0 THEN 1 ELSE 0 END) AS FLOAT)
/ COUNT(*) AS Cancellation_Rate
FROM Transactions t
LEFT JOIN Products p
ON LTRIM(RTRIM(t.StockCode)) = LTRIM(RTRIM(p.StockCode))
GROUP BY
t.StockCode,
p.ProductName,
p.Category
ORDER BY Cancellation_Rate DESC;
--Average Order Value (AOV) per Customer
SELECT
CustomerID,
AVG(OrderValue) AS Avg_Order_Value
FROM (
SELECT
CustomerID,
Invoice,
SUM(Revenue) AS OrderValue
FROM final_transactions
GROUP BY CustomerID, Invoice
) sub
GROUP BY CustomerID
ORDER BY Avg_Order_Value DESC;
--Repeat vs One-Time Buyers
SELECT
Buyer_Type,
COUNT(*) AS Customer_Count
FROM (
SELECT
CustomerID,
CASE
WHEN COUNT(DISTINCT Invoice) = 1 THEN 'One-Time Buyer'
ELSE 'Repeat Buyer'
END AS Buyer_Type
FROM final_transactions
GROUP BY CustomerID
) sub
GROUP BY Buyer_Type;
--Pareto Analysis (Top Customers Contribution)
WITH customer_spend AS (
SELECT
CustomerID,
SUM(Revenue) AS Total_Spend
FROM final_transactions
GROUP BY CustomerID
),
ranked AS (
SELECT
CustomerID,
Total_Spend,
NTILE(5) OVER (ORDER BY Total_Spend DESC) AS Spend_Group
FROM customer_spend
)
SELECT
Spend_Group,
SUM(Total_Spend) AS Group_Revenue
FROM ranked
GROUP BY Spend_Group
ORDER BY Spend_Group;
--Cohort Analysis (Customer Retention)
WITH first_purchase AS (
SELECT
CustomerID,
MIN(FORMAT(InvoiceDate, 'yyyy-MM')) AS First_Month
FROM final_transactions
GROUP BY CustomerID
),
cohort_data AS (
SELECT
t.CustomerID,
f.First_Month,
FORMAT(t.InvoiceDate, 'yyyy-MM') AS Activity_Month
FROM final_transactions t
JOIN first_purchase f
ON t.CustomerID = f.CustomerID
)
SELECT
First_Month,
Activity_Month,
COUNT(DISTINCT CustomerID) AS Customers
FROM cohort_data
GROUP BY First_Month, Activity_Month
ORDER BY First_Month, Activity_Month;
--Revenue by Product Category
SELECT
Category,
SUM(Revenue) AS Total_Revenue
FROM final_transactions
GROUP BY Category
ORDER BY Total_Revenue DESC;
--Channel-wise Revenue & Orders
SELECT
Channel,
COUNT(DISTINCT Invoice) AS Total_Orders,
SUM(Revenue) AS Total_Revenue
FROM final_transactions
GROUP BY Channel
ORDER BY Total_Revenue DESC;
SELECT * FROM final_transactions;