-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3_KPIS.SQL
More file actions
375 lines (285 loc) · 8.54 KB
/
3_KPIS.SQL
File metadata and controls
375 lines (285 loc) · 8.54 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
--------------------------------------------------------------- KPI's ----------------------------------------------------------------------
USE [Swiggy Database]
--------------------------------------------------------------------------------------------------------------------------------------------
-- 1. Total Oder
SELECT COUNT(*) AS Total_Orders
FROM dbo.[Fact_Order]
-- 2. Total Revenew in ₹-M (GMV = Gross Merchandise Value)
SELECT
FORMAT(SUM(CONVERT(float,price_INR))/1000000,'N2') + ' INR Mill.' AS Total_Revenew_in_Mill
FROM dbo.[Fact_Order]
-- 3. Average Order Value (AOV)
SELECT
(SUM(Price_INR)*1.0 / COUNT(DISTINCT Order_ID)) AS AVG_Order_Value
FROM dbo.[Fact_Order]
-- 4. Orders per Day
SELECT
d.full_date,
COUNT(DISTINCT f.order_ID)
FROM dbo.[Fact_Order] f
JOIN dbo.[Dim_Date] d
ON
f.date_ID = d.date_ID
GROUP BY d.full_date
ORDER BY d.full_date
--5. Orders per Quarter
SELECT
d.[year],
d.[quater],
COUNT(DISTINCT f.order_ID) AS order_per_quater
FROM dbo.[Fact_Order] f
JOIN dbo.[Dim_Date] d
ON
f.date_ID = d.date_ID
GROUP BY d.[year],d.[quater]
ORDER BY d.[year],d.[quater]
--6. Orders per Month
SELECT
d.[year],
d.[month],
COUNT(DISTINCT f.order_ID) AS order_per_month
FROM dbo.[Fact_Order] f
JOIN dbo.[Dim_Date] d
ON
f.date_ID = d.date_ID
GROUP BY d.[year],d.[month]
ORDER BY d.[year],d.[month]
--7. Revenue by Month (Base for Growth Metrics)
SELECT
d.[year],
d.[month],
FORMAT(SUM(CONVERT(float,f.price_INR))/1000000,'N2')+ ' M' AS Revenew_per_Month_in_Million
FROM dbo.[Fact_Order] f
JOIN dbo.[Dim_Date] d
ON f.date_ID = d.date_ID
GROUP BY d.[year],d.[month]
ORDER BY d.[year],d.[month]
--8. Month-over-Month (MoM) Revenue Growth %
WITH Monthly_Revenue AS (
SELECT
d.[year],
d.[month],
SUM(f.[Price_INR]) AS revenue
FROM dbo.[Fact_Order] f
JOIN dbo.[Dim_Date] d
ON f.Date_ID = d.Date_ID
GROUP BY d.[year], d.[month]
)
SELECT
[year],
[month],
[revenue],
LAG(revenue) OVER(ORDER BY [year],[month]) AS prev_revenew,
(revenue-LAG(revenue) OVER(ORDER BY [year],[month]))*100/LAG(revenue) OVER(ORDER BY [year],[month]) AS monthly_groth_in_pct
FROM Monthly_Revenue
ORDER BY[year],[month]
--9. Quarter-over-Quarter (QoQ) Revenue Growth %
WITH Quaterly_curr_Revenue AS (
SELECT
d.[year],
d.[quater],
SUM(f.[Price_INR]) AS curr_revenue
FROM dbo.[Fact_Order] f
JOIN dbo.[Dim_Date] d
ON f.Date_ID = d.Date_ID
GROUP BY d.[year], d.[quater]
),
Quaterly_prev_Revenue AS(
SELECT
[year],
[quater],
curr_revenue,
LAG(curr_revenue) OVER(ORDER BY [year],[quater]) AS prev_revenew
FROM Quaterly_curr_Revenue
)
SELECT
[year],
[quater],
[curr_revenue],
[prev_revenew],
([curr_revenue]-[prev_revenew])*100.0/[prev_revenew] AS quaterly_growth
FROM Quaterly_prev_Revenue
--10. Year-over-Year (YoY) Revenue Growth %
-- There is only 1 year 2025 , no need to calculate YoY
----------------------------------------------------------------Location Performance KPIs---------------------------------------------------
--1. Orders by Location (State / City)
SELECT
l.[state],
l.[city],
COUNT(DISTINCT f.Order_ID) AS total_orders
FROM dbo.[Fact_Order] f
JOIN dbo.[Dim_Location] l
ON f.[location_ID] = l.[location_ID]
GROUP BY L.[state],L.[city]
ORDER BY total_orders DESC
--2. Revenue by State / City
SELECT
l.[state],
l.[city],
FORMAT(SUM(CONVERT(float,f.price_INR))/1000000,'N2')+ ' M'AS revenue_per_state_city
FROM dbo.[Fact_Order] f
JOIN dbo.[Dim_Location] l
ON f.[location_ID] = l.[location_ID]
GROUP BY L.[state],L.[city]
ORDER BY revenue_per_state_city DESC
--3. Average Order Value (AOV) by Location
SELECT
l.[state],
l.[city],
SUM(f.price_INR)/COUNT(DISTINCT f.Order_ID) AS avg_order_value_by_location
FROM dbo.[Fact_Order] f
JOIN dbo.[Dim_Location] l
ON f.[location_ID] = l.[location_ID]
GROUP BY l.[state],l.[city]
ORDER BY avg_order_value_by_location DESC
--4. Location Revenue Contribution %
WITH Location_Revenue AS (
SELECT
l.State,
l.City,
SUM(f.Price_INR) AS revenue
FROM dbo.Fact_Order f
JOIN dbo.Dim_Location l
ON f.Location_ID = l.Location_ID
GROUP BY l.State, l.City
),
Total_Revenue AS (
SELECT SUM(revenue) AS total_revenue
FROM Location_Revenue
)
SELECT
lr.State,
lr.City,
lr.revenue,
lr.revenue * 100.0 / tr.total_revenue AS revenue_contribution_pct
FROM Location_Revenue lr
CROSS JOIN Total_Revenue tr
ORDER BY revenue_contribution_pct DESC;
--5. Top 10 Performing Cities (by Revenue)
SELECT TOP 10
l.[city],
l.[state],
SUM(f.[price_INR]) AS total_revenew
FROM dbo.[Fact_Order] f
JOIN dbo.[Dim_Location] l
ON f.[location_ID] = l.[location_ID]
GROUP BY l.[city],l.[state]
ORDER BY total_revenew DESC;
--6. Bottom 10 Performing Cities (by Revenue)
SELECT TOP 10
l.[city],
l.[state],
SUM(f.[price_INR]) AS total_revenew
FROM dbo.[Fact_Order] f
JOIN dbo.[Dim_Location] l
ON f.[location_ID] = l.[location_ID]
GROUP BY l.[city],l.[state]
ORDER BY total_revenew ASC;
----------------------------------------------------------------Restaurant Performance KPIs---------------------------------------------------
--1. Orders per Restaurant
SELECT
r.[restaurant_ID],
r.[restaurent_name],
COUNT(DISTINCT f.[order_ID]) AS Total_orders_per_Restaurent
FROM DBO.[Fact_Order] f
JOIN DBO.[Dim_Restaurant] r
ON f.[restaurent_ID] = r.[restaurant_ID]
GROUP BY r.[restaurant_ID] , r.[restaurent_name]
ORDER BY Total_orders_per_Restaurent DESC
--2. Revenue per Restaurant
SELECT
r.[restaurant_ID],
r.[restaurent_name],
SUM(f.Price_INR) AS Total_Revenue
FROM dbo.[Fact_Order] f
JOIN dbo.[Dim_Restaurant] r
ON f.[restaurent_ID] = r.[restaurant_ID]
GROUP BY r.[restaurant_ID], r.[restaurent_name]
ORDER BY Total_Revenue DESC;
--3. Avg Revenue per Order per Restaurant (AOV)
SELECT
r.[restaurant_ID],
r.[restaurent_name],
SUM(f.[Price_INR]) * 1.0 / COUNT(DISTINCT f.[Order_ID]) AS avg_revenue_per_order
FROM dbo.[Fact_Order] f
JOIN dbo.[Dim_Restaurant] r
ON f.[restaurent_ID] = r.[restaurant_ID]
GROUP BY r.[restaurant_ID], r.[restaurent_name]
ORDER BY avg_revenue_per_order DESC;
--4. Top 10 restaurants’ share of total revenue
WITH Restaurant_Revenue AS (
SELECT
r.[restaurant_ID],
r.[restaurent_name],
SUM(f.[Price_INR]) AS revenue
FROM dbo.[Fact_Order] f
JOIN dbo.[Dim_Restaurant] r
ON f.[restaurent_ID] = r.[restaurant_ID]
GROUP BY r.[restaurant_ID], r.[restaurent_name]
),
Ranked_Restaurants AS (
SELECT
*,
ROW_NUMBER() OVER (ORDER BY revenue DESC) AS rn
FROM Restaurant_Revenue
),
Total_Revenue AS (
SELECT SUM(revenue) AS total_revenue
FROM Restaurant_Revenue
)
SELECT
SUM(rr.revenue) * 100.0 / MAX(tr.total_revenue)
AS top_10_revenue_concentration_pct
FROM Ranked_Restaurants rr
CROSS JOIN Total_Revenue tr
WHERE rr.rn <= 10;
----------------------------------------------------------------Category Based KPIs---------------------------------------------------
-- 1. Orders by Category
SELECT
c.[catagory],
COUNT(DISTINCT f.[Order_ID]) AS total_orders
FROM dbo.[Fact_Order] f
JOIN dbo.[Dim_Catagory] c
ON f.[catagory_ID] = c.[catagory_ID]
GROUP BY c.[catagory]
ORDER BY total_orders DESC;
--2. Revenue by Category
SELECT
c.[catagory],
SUM(f.[Price_INR]) AS total_revenue
FROM dbo.[Fact_Order] f
JOIN dbo.[Dim_Catagory] c
ON f.[catagory_ID] = c.[catagory_ID]
GROUP BY c.[catagory]
ORDER BY total_revenue DESC;
-- 3. Category Mix %
WITH Category_Revenue AS (
SELECT
c.[catagory],
SUM(f.Price_INR) AS revenue
FROM dbo.[Fact_Order] f
JOIN dbo.[Dim_Catagory] c
ON f.[catagory_ID] = c.[catagory_ID]
GROUP BY c.[catagory]
),
Total_Revenue AS (
SELECT SUM(revenue) AS total_revenue
FROM Category_Revenue
)
SELECT
cr.[catagory],
cr.revenue,
cr.revenue * 100.0 / tr.total_revenue AS category_mix_pct
FROM Category_Revenue cr
CROSS JOIN Total_Revenue tr
ORDER BY category_mix_pct DESC;
--4. Avg Price per Category (AOV by Category)
SELECT
c.catagory,
SUM(f.Price_INR) * 1.0 / COUNT(DISTINCT f.Order_ID) AS avg_price_per_order
FROM dbo.[Fact_Order] f
JOIN dbo.[Dim_Catagory] c
ON f.[catagory_ID] = c.[catagory_ID]
GROUP BY c.[catagory]
ORDER BY avg_price_per_order DESC;
--------------------------------------------------------------------------------------------------------------------------------------------