-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofit and revenue analysis.sql
230 lines (167 loc) · 5.28 KB
/
profit and revenue analysis.sql
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
use pizza;
-- About dataset
select * from pizza_sales ;
-- Disable safe update mode
SET SQL_SAFE_UPDATES = 0;
-- Update the order_date column to the correct format
UPDATE pizza_sales
SET order_date = STR_TO_DATE(order_date, '%d-%m-%Y')
WHERE order_date LIKE '%-%-%';
-- modify column datatype into date
Alter table pizza_sales
modify column order_date DATE;
-- modify column datatype into time
Alter table pizza_sales
modify column order_time TIME;
-- Total orders
SELECT COUNT(*) AS total_orders FROM pizza_sales;
-- Types of pizzas catagory
select distinct(pizza_category) from pizza_sales;
-- how many different types of pizzas
select distinct pizza_name from pizza_sales;
-- total no of pizzas sell
select sum(quantity) as total_pizzas from pizza_sales;
-- pizza with highest price
select pizza_name as maximum_cost_pizza ,unit_price
from pizza_sales
order by unit_price desc
limit 1;
-- pizza with lowest price
select pizza_name as minimum_cost_pizza ,unit_price
from pizza_sales
order by unit_price
limit 1;
-- Revenue Analysis --
-- Total revenue
select sum(total_price) as total_revenue from pizza_sales;
-- Hourly Revenue Analysis
SELECT
EXTRACT(HOUR FROM order_time) AS hour_of_day,
SUM(total_price) AS total_revenue
FROM
pizza_sales
GROUP BY
hour_of_day
ORDER BY
total_revenue desc ;
-- which day of which month of which year had the highest rvenue
SELECT
YEAR(order_date) AS year,
MONTH(order_date) AS month,
DAY(order_date) AS day,
SUM(total_price) AS total_revenue
FROM
pizza_sales
GROUP BY
year, month, day
order by total_revenue desc
limit 1;
-- which month has highest revenue
select monthname(order_date),
sum(total_price) as revenue_by_month from pizza_sales
group by monthname(order_date)
order by revenue_by_month desc
limit 1;
-- Which days of the week having the highest revenue?
SELECT DATE_FORMAT(order_date, '%W') AS day_of_week, sum(total_price) AS revenue
FROM pizza_sales
GROUP BY day_of_week
ORDER BY revenue DESC ;
-- which type of pizza( name ) has highest revenue
select pizza_name,
sum(total_price) as revenue_by_name from pizza_sales
group by pizza_name
order by revenue_by_name desc
limit 1;
-- What is the total revenue of pizza across different categories?
select pizza_category,
sum(total_price) as revenue_by_category from pizza_sales
group by pizza_category
order by revenue_by_category desc ;
-- What is the total revenue of pizza across different sizes?
select pizza_size,
sum(total_price) as revenue_by_size from pizza_sales
group by pizza_size
order by revenue_by_size desc;
-- revenue according to season
select
CASE
WHEN MONTH(order_date) in (3,4,5) then 'spring'
WHEN MONTH(order_date) in (6,7,8) then 'summer'
WHEN MONTH(order_date) in (12,1,2) then 'winter'
else 'fall'
end as season ,
sum(total_price) as season_revenue
from pizza_sales
group by season
order by season_revenue;
-- profit --
-- Profit Margin Calculation
SELECT
pizza_sales.pizza_name_id,
pizza_sales.pizza_name,
SUM(pizza_sales.total_price) AS total_revenue,
SUM( pizza_cost.cost * pizza_sales.quantity) AS total_cost,
SUM(pizza_sales.total_price) - SUM( pizza_cost.cost *pizza_sales.quantity) AS total_profit,
(SUM(pizza_sales.total_price) - SUM( pizza_cost.cost * pizza_sales.quantity)) / SUM(pizza_sales.total_price) * 100 AS profit_margin_percentage
FROM
pizza_sales
JOIN
pizza_cost ON pizza_sales.pizza_name_id = pizza_cost.pizza_name_id
GROUP BY
pizza_sales.pizza_name_id, pizza_sales.pizza_name
ORDER BY
total_profit DESC limit 3;
-- Profit by Category
SELECT
pizza_sales.pizza_category,
SUM(pizza_sales.total_price) AS total_revenue,
SUM(pizza_cost.cost * pizza_sales.quantity) AS total_cost,
SUM(pizza_sales.total_price) - SUM(pizza_cost.cost * pizza_sales.quantity) AS total_profit
FROM
pizza_sales
JOIN
pizza_cost ON pizza_sales.pizza_name_id = pizza_cost.pizza_name_id
GROUP BY
pizza_sales.pizza_category
ORDER BY
total_profit DESC;
-- Which month has the highest Profit?
SELECT
month(order_date) as month,
SUM(pizza_sales.total_price) AS total_revenue,
SUM(pizza_cost.cost * pizza_sales.quantity) AS total_cost,
SUM(pizza_sales.total_price) - SUM(pizza_cost.cost * pizza_sales.quantity) AS total_profit
FROM
pizza_sales
JOIN
pizza_cost ON pizza_sales.pizza_name_id = pizza_cost.pizza_name_id
GROUP BY
month
ORDER BY
total_profit desc
limit 1;
-- Profit by Time of Day
select
case
when hour(order_time) between 0 and 5
then 'Night'
when hour(order_time) between 6 and 11
then 'Morning'
when hour(order_time) between 12 and 17
then 'Afternoon'
ELSE
'Evening'
END as Time_of_day,
SUM(pizza_sales.total_price) AS total_revenue,
SUM(pizza_cost.cost * pizza_sales.quantity) AS total_cost,
SUM(pizza_sales.total_price) - SUM(pizza_cost.cost * pizza_sales.quantity) AS total_profit
FROM
pizza_sales
JOIN
pizza_cost ON pizza_sales.pizza_name_id = pizza_cost.pizza_name_id
GROUP BY
time_of_day
ORDER BY
total_profit DESC
limit 1;