-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwalmart_project.sql
More file actions
341 lines (298 loc) · 6.85 KB
/
walmart_project.sql
File metadata and controls
341 lines (298 loc) · 6.85 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
use project_1;
select * from walmartsales_dataset;
#TASK 1
-- Step 1: Calculate monthly total sales per branch
WITH monthly_sales AS (
SELECT
Branch,
DATE_FORMAT(STR_TO_DATE(Date, '%m/%d/%Y'), '%Y-%m-01') AS sales_month,
SUM(Total) AS monthly_total
FROM
walmartsales_dataset
GROUP BY
Branch, DATE_FORMAT(STR_TO_DATE(Date, '%m/%d/%Y'), '%Y-%m-01')
),
-- Step 2: Calculate month-over-month growth using LAG
sales_growth AS (
SELECT
Branch,
sales_month,
monthly_total,
LAG(monthly_total) OVER (PARTITION BY Branch ORDER BY sales_month) AS prev_month_total
FROM
monthly_sales
),
-- Step 3: Calculate growth rate
growth_rate AS (
SELECT
Branch,
sales_month,
monthly_total,
prev_month_total,
CASE
WHEN prev_month_total > 0 THEN (monthly_total - prev_month_total) / prev_month_total
ELSE NULL
END AS monthly_growth_rate
FROM
sales_growth
),
-- Step 4: Average growth rate per branch
avg_growth AS (
SELECT
Branch,
ROUND(AVG(monthly_growth_rate), 4) AS avg_monthly_growth
FROM
growth_rate
WHERE
monthly_growth_rate IS NOT NULL
GROUP BY
Branch
)
-- Step 5: Get top branch
SELECT
Branch,
avg_monthly_growth
FROM
avg_growth
ORDER BY
avg_monthly_growth DESC
LIMIT 1;
#TASK 2
select * from walmartsales_dataset;
-- Step 1: Calculate profit per product line per branch
WITH product_profit AS (
SELECT
Branch,
"Product line" AS product_line,
SUM("gross income" - cogs) AS total_profit
FROM
walmartsales_dataset
GROUP BY
Branch, "Product line"
),
-- Step 2: Rank product lines within each branch by profit
ranked_products AS (
SELECT
Branch,
product_line,
total_profit,
RANK() OVER (PARTITION BY Branch ORDER BY total_profit DESC) AS profit_rank
FROM
product_profit
)
-- Step 3: Select the most profitable product line per branch
SELECT
Branch,
product_line,
total_profit
FROM
ranked_products
WHERE
profit_rank = 1
ORDER BY
Branch;
#TASK 3
WITH customer_spending AS (
SELECT
"Customer ID" AS customer_id,
SUM(Total) AS total_spent
FROM
walmartsales_dataset
GROUP BY
"Customer ID"
),
-- Step 2: Rank customers by total spending and calculate percentiles
ranked_customers AS (
SELECT
customer_id,
total_spent,
NTILE(3) OVER (ORDER BY total_spent DESC) AS spending_tier
FROM
customer_spending
)
-- Step 3: Label the segments
SELECT
customer_id,
total_spent,
CASE spending_tier
WHEN 1 THEN 'High Spender'
WHEN 2 THEN 'Medium Spender'
WHEN 3 THEN 'Low Spender'
END AS spending_segment
FROM
ranked_customers
ORDER BY
total_spent DESC;
#TASK 4
-- Step 1: Calculate average and standard deviation for each product line
WITH product_stats AS (
SELECT
"Product line",
AVG(Total) AS avg_total,
STDDEV(Total) AS std_total
FROM
walmartsales_dataset
GROUP BY
"Product line"
),
-- Step 2: Join with original data to compare each transaction to the stats
transaction_analysis AS (
SELECT
w.*,
ps.avg_total,
ps.std_total,
-- Z-score calculation
(Total - ps.avg_total) / ps.std_total AS z_score
FROM
walmartsales_dataset w
JOIN
product_stats ps
ON w."Product line" = ps."Product line"
)
-- Step 3: Filter out anomalies (z-score > 2 or < -2)
SELECT
"Invoice ID",
"Product line",
Branch,
Total,
ROUND(avg_total, 2) AS avg_product_total,
ROUND(z_score, 2) AS z_score,
CASE
WHEN z_score > 2 THEN 'High Anomaly'
WHEN z_score < -2 THEN 'Low Anomaly'
END AS anomaly_type
FROM
transaction_analysis
WHERE
ABS(z_score) > 2
ORDER BY
ABS(z_score) DESC;
#TASK 5
-- Step 1: Count transactions per payment method in each city
WITH payment_counts AS (
SELECT
City,
Payment,
COUNT(*) AS payment_count
FROM
walmartsales_dataset
GROUP BY
City, Payment
),
-- Step 2: Rank payment methods by usage within each city
ranked_payments AS (
SELECT
City,
Payment,
payment_count,
RANK() OVER (PARTITION BY City ORDER BY payment_count DESC) AS rank_in_city
FROM
payment_counts
)
-- Step 3: Get the most popular payment method per city
SELECT
City,
Payment AS most_popular_payment,
payment_count
FROM
ranked_payments
WHERE
rank_in_city = 1
ORDER BY
City;
#TASK 6
-- Step 1: Extract month from the Date column and group by Gender
SELECT
DATE_FORMAT(STR_TO_DATE(Date, '%m/%d/%Y'), '%Y-%m') AS sales_month,
Gender,
SUM(Total) AS total_sales
FROM
walmartsales_dataset
GROUP BY
sales_month, Gender
ORDER BY
sales_month, Gender;
#TASK 7
-- Step 1: Sum total sales per product line and customer type
WITH product_sales AS (
SELECT
"Customer type",
"Product line",
SUM(Total) AS total_sales
FROM
walmartsales_dataset
GROUP BY
"Customer type", "Product line"
),
-- Step 2: Rank product lines per customer type by total sales
ranked_sales AS (
SELECT
"Customer type",
"Product line",
total_sales,
RANK() OVER (PARTITION BY "Customer type" ORDER BY total_sales DESC) AS rank
FROM
product_sales
)
-- Step 3: Select the top product line for each customer type
SELECT
"Customer type",
"Product line",
total_sales
FROM
ranked_sales
WHERE
rank = 1;
#TASK 8
-- Step 1: Convert Date to proper format and join each customer’s transactions to themselves
WITH parsed_data AS (
SELECT
`Customer ID`,
STR_TO_DATE(`Date`, '%m/%d/%Y') AS txn_date,
`Invoice ID`
FROM
walmartsales_dataset
),
-- Step 2: Self-join on Customer ID to compare dates
repeat_customers AS (
SELECT
a.`Customer ID`,
a.txn_date AS first_purchase,
b.txn_date AS repeat_purchase,
DATEDIFF(b.txn_date, a.txn_date) AS days_between
FROM
parsed_data a
JOIN
parsed_data b
ON a.`Customer ID` = b.`Customer ID`
AND b.txn_date > a.txn_date
AND DATEDIFF(b.txn_date, a.txn_date) <= 30
)
-- Step 3: Get distinct repeat customers
SELECT DISTINCT
`Customer ID`
FROM
repeat_customers
ORDER BY
`Customer ID`;
#TASK 9
SELECT
`Customer ID`,
SUM(`Total`) AS total_revenue
FROM
walmartsales_dataset
GROUP BY
`Customer ID`
ORDER BY
total_revenue DESC
LIMIT 5;
#TASK 10
SELECT
DAYNAME(STR_TO_DATE(`Date`, '%m/%d/%Y')) AS day_of_week,
SUM(`Total`) AS total_sales
FROM
walmartsales_dataset
GROUP BY
day_of_week
ORDER BY
total_sales DESC;