-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPSQL.sql
More file actions
164 lines (123 loc) · 3.1 KB
/
PSQL.sql
File metadata and controls
164 lines (123 loc) · 3.1 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
-- Business Problems
--Q.1 Find different payment method and number of transactions, number of qty sold
SELECT
payment_method,
COUNT(*) as no_payments,
SUM(quantity) as no_qty_sold
FROM walmart
GROUP BY payment_method
-- Project Question #2
-- Identify the highest-rated category in each branch, displaying the branch, category
-- AVG RATING
SELECT *
FROM
( SELECT
branch,
category,
AVG(rating) as avg_rating,
RANK() OVER(PARTITION BY branch ORDER BY AVG(rating) DESC) as rank
FROM walmart
GROUP BY 1, 2
)
WHERE rank = 1
-- Q.3 Identify the busiest day for each branch based on the number of transactions
SELECT *
FROM
(SELECT
branch,
TO_CHAR(TO_DATE(date, 'DD/MM/YY'), 'Day') as day_name,
COUNT(*) as no_transactions,
RANK() OVER(PARTITION BY branch ORDER BY COUNT(*) DESC) as rank
FROM walmart
GROUP BY 1, 2
)
WHERE rank = 1
-- Q. 4
-- Calculate the total quantity of items sold per payment method. List payment_method and total_quantity.
SELECT
payment_method,
-- COUNT(*) as no_payments,
SUM(quantity) as no_qty_sold
FROM walmart
GROUP BY payment_method
-- Q.5
-- Determine the average, minimum, and maximum rating of category for each city.
-- List the city, average_rating, min_rating, and max_rating.
SELECT
city,
category,
MIN(rating) as min_rating,
MAX(rating) as max_rating,
AVG(rating) as avg_rating
FROM walmart
GROUP BY 1, 2
-- Q.6
-- Calculate the total profit for each category by considering total_profit as
-- (unit_price * quantity * profit_margin).
-- List category and total_profit, ordered from highest to lowest profit.
SELECT
category,
SUM(total) as total_revenue,
SUM(total * profit_margin) as profit
FROM walmart
GROUP BY 1
-- Q.7
-- Determine the most common payment method for each Branch.
-- Display Branch and the preferred_payment_method.
WITH cte
AS
(SELECT
branch,
payment_method,
COUNT(*) as total_trans,
RANK() OVER(PARTITION BY branch ORDER BY COUNT(*) DESC) as rank
FROM walmart
GROUP BY 1, 2
)
SELECT *
FROM cte
WHERE rank = 1
-- #9 Identify 5 branch with highest decrese ratio in
-- revevenue compare to last year(current year 2023 and last year 2022)
-- rdr == last_rev-cr_rev/ls_rev*100
SELECT *,
EXTRACT(YEAR FROM TO_DATE(date, 'DD/MM/YY')) as formated_date
FROM walmart
-- 2022 sales
WITH revenue_2022
AS
(
SELECT
branch,
SUM(total) as revenue
FROM walmart
WHERE EXTRACT(YEAR FROM TO_DATE(date, 'DD/MM/YY')) = 2022 -- psql
-- WHERE YEAR(TO_DATE(date, 'DD/MM/YY')) = 2022 -- mysql
GROUP BY 1
),
revenue_2023
AS
(
SELECT
branch,
SUM(total) as revenue
FROM walmart
WHERE EXTRACT(YEAR FROM TO_DATE(date, 'DD/MM/YY')) = 2023
GROUP BY 1
)
SELECT
ls.branch,
ls.revenue as last_year_revenue,
cs.revenue as cr_year_revenue,
ROUND(
(ls.revenue - cs.revenue)::numeric/
ls.revenue::numeric * 100,
2) as rev_dec_ratio
FROM revenue_2022 as ls
JOIN
revenue_2023 as cs
ON ls.branch = cs.branch
WHERE
ls.revenue > cs.revenue
ORDER BY 4 DESC
LIMIT 5