Repository navigation
Expand file tree
/
Copy pathSQL Query.sql
More file actions
306 lines (143 loc) · 4.51 KB
/
Copy pathSQL Query.sql
File metadata and controls
306 lines (143 loc) · 4.51 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
----------------------------------------------------------------Business Problems------------------------------------------------------------------------------------
-- Add New Column week_number
ALTER TABLE walmart_data ADD COLUMN week_number INT;
UPDATE walmart_data
SET week_number = EXTRACT(WEEK FROM "date"::DATE);
-- Add New Shift Mode Column.
ALTER TABLE walmart_data ADD COLUMN shift_mode VARCHAR(100)
UPDATE walmart_data AS w
SET shift_mode = CASE
WHEN w."time"::TIME < TIME '12:00' THEN 'Morning'
WHEN w."time"::TIME >= TIME '12:00' AND w."time"::TIME < TIME '15:00' THEN 'Noon'
WHEN w."time"::TIME >= TIME '15:00' AND w."time"::TIME < TIME '18:00' THEN 'Evening'
ELSE 'Night'
END;
-- Calculate The Total Transaction
SELECT COUNT(*) FROM walmart_data;
-- How many Branch
SELECT COUNT(DISTINCT "Branch") FROM walmart_data;
-- Q1. What are the different payment methods, and how many transactions and
-- items were sold with each method?
SELECT
"payment_method" AS payment_method,
COUNT(*) AS transactions,
SUM("quantity") AS item_sold
FROM walmart_data
GROUP BY
payment_method
ORDER BY
item_sold DESC;
-- Q2. Which category received the highest average rating in each branch?
WITH avg_ratings AS (
SELECT
"category" AS category,
"Branch" AS branch,
ROUND(AVG("rating")::numeric, 2) AS avg_rating,
ROW_NUMBER() OVER (PARTITION BY "Branch" ORDER BY AVG("rating") DESC) AS rank
FROM walmart_data
GROUP BY category, branch
)
SELECT category, branch, avg_rating
FROM avg_ratings
WHERE rank = 1;
-- Q3. What is the busiest day of the week for each branch based on transaction volume?
WITH busiest_day AS(
SELECT
"Branch" AS branch,
"Day" AS days,
"week_number" AS week_num,
-- "Branch" AS branch,
COUNT(*) AS transaction_vol,
ROW_NUMBER() OVER(PARTITION BY "week_number","Branch" ORDER BY COUNT(*) DESC) AS rank
FROM walmart_data
GROUP BY branch,days,week_num
)
SELECT
branch,
days,
week_num,
transaction_vol
FROM busiest_day
WHERE
rank = 1;
-- Q4. How many items were sold through each payment method?
SELECT
"payment_method" AS payment_method,
SUM("quantity") AS item_sold
FROM
walmart_data
GROUP BY
payment_method;
-- Q5. What are the average, minimum, and maximum ratings for each category in
-- each city?
SELECT
"category" AS category,
"City" AS city,
ROUND(AVG("rating")::numeric,2) AS avg_rating,
MIN("rating") AS minimum_rating,
MAX("rating") AS maximum_rating
FROM walmart_data
GROUP BY
category,city
ORDER BY city
-- Q6. What is the total profit for each category, ranked from highest to lowest?
SELECT
"category" AS category,
SUM("Total_price") AS total_profit
FROM
walmart_data
GROUP BY
category
ORDER BY
total_profit DESC;
-- Q7. What is the most frequently used payment method in each branch?
WITH frequently_used_payment AS (
SELECT
"Branch" AS branch,
"payment_method" AS payment_method,
COUNT(*) AS number_of_trans,
ROW_NUMBER() OVER(PARTITION BY "Branch" ORDER BY COUNT(*) DESC) AS rank
FROM walmart_data
GROUP BY
branch,payment_method
)
SELECT
branch,
payment_method,
number_of_trans
FROM
frequently_used_payment
WHERE rank = 1;
-- Q8. How many transactions occur in each shift (Morning, Afternoon, Evening)
-- across branches?
SELECT
"Branch" AS branch,
"shift_mode" AS shit_mode,
COUNT(*) AS num_trans
FROM
walmart_data
GROUP BY
branch,shift_mode
ORDER BY
branch;
-- Q9. Which branches experienced the largest decrease in revenue compared to
-- the previous year?
SELECT
curr."Branch" AS branch,
prev."Year" AS previous_year,
curr."Year" AS current_year,
prev.total_revenue AS revenue_previous_year,
curr.total_revenue AS revenue_current_year,
(curr.total_revenue - prev.total_revenue) AS revenue_change
FROM (
SELECT "Branch", "Year", SUM("Total_price") AS total_revenue
FROM walmart_data
GROUP BY "Branch", "Year"
) curr
JOIN (
SELECT "Branch", "Year", SUM("Total_price") AS total_revenue
FROM walmart_data
GROUP BY "Branch", "Year"
) prev
ON curr."Branch" = prev."Branch" AND curr."Year" = prev."Year" + 1
ORDER BY revenue_change,branch ASC;