-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUPS_Logistics_Analysis.sql
More file actions
644 lines (559 loc) · 17.6 KB
/
UPS_Logistics_Analysis.sql
File metadata and controls
644 lines (559 loc) · 17.6 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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
/* =========================================================
PROJECT: Logistics Optimization for Delivery Routes (UPS)
DATABASE: ups_logistics
AUTHOR: Manas Patnaik
DESCRIPTION:
SQL-based analysis to clean data, analyze delivery delays,
optimize routes, and evaluate warehouse & agent performance.
========================================================= */
USE ups_logistics;
-- =========================================================
-- TASK 1: DATA CLEANING & PREPARATION
-- =========================================================
/* ---------------------------------------------------------
1.1 Duplicate Order_ID Check
Purpose: Ensure each order is unique before analysis
---------------------------------------------------------- */
SELECT COUNT(*) AS total_rows,
COUNT(DISTINCT Order_ID) AS unique_orders
FROM orders;
/*
Observation:
- total_rows = unique_orders
- No duplicate Order_ID records found
*/
/* ---------------------------------------------------------
1.2 Traffic Delay Null Check (Routes Table)
Purpose: Verify completeness of Traffic_Delay_Min
---------------------------------------------------------- */
SELECT COUNT(*) AS total_routes,
COUNT(Traffic_Delay_Min) AS non_null_delay
FROM routes;
/*
Observation:
- No NULL values found in Traffic_Delay_Min
- No replacement required
*/
/* ---------------------------------------------------------
1.3 Date Format Standardization
Purpose: Convert text-based date columns to DATE format
---------------------------------------------------------- */
ALTER TABLE orders
MODIFY Order_Date DATE,
MODIFY Expected_Delivery_Date DATE,
MODIFY Actual_Delivery_Date DATE;
ALTER TABLE shipment_tracking
MODIFY Checkpoint_Time DATE;
/*
Result:
- All date columns standardized to YYYY-MM-DD
*/
/* ---------------------------------------------------------
1.4 Invalid Delivery Date Flagging
Purpose: Identify records where delivery date < order date
---------------------------------------------------------- */
SELECT
Order_ID,
Order_Date,
Actual_Delivery_Date,
CASE
WHEN Actual_Delivery_Date < Order_Date
THEN 'INVALID_RECORD'
ELSE 'VALID_RECORD'
END AS Record_Status
FROM orders;
/*
Observation:
- No invalid records found
*/
-- =========================================================
-- TASK 2: DELIVERY DELAY ANALYSIS
-- =========================================================
/* ---------------------------------------------------------
2.1 Delivery Delay Calculation
Purpose: Calculate delivery delay (in days) for each order
based on expected vs actual delivery dates
--------------------------------------------------------- */
SELECT
Order_ID,
Route_ID,
Warehouse_ID,
Expected_Delivery_Date,
Actual_Delivery_Date,
GREATEST(
DATEDIFF(Actual_Delivery_Date, Expected_Delivery_Date),
0
) AS Delivery_Delay_Days
FROM orders;
/*
Observation:
- Delivery delay calculated in days for each order
- Early or on-time deliveries assigned a delay of 0 days
*/
/* ---------------------------------------------------------
2.2 Top 10 Delayed Routes
Purpose: Identify routes with highest average delivery delay
--------------------------------------------------------- */
SELECT
Route_ID,
ROUND(
AVG(DATEDIFF(Actual_Delivery_Date, Expected_Delivery_Date)),
2
) AS Avg_Delay_Days
FROM orders
WHERE Actual_Delivery_Date > Expected_Delivery_Date
GROUP BY Route_ID
ORDER BY Avg_Delay_Days DESC
LIMIT 10;
/*
Observation:
- Routes ranked based on average delivery delay
- Top 10 consistently underperforming routes identified
*/
/* ---------------------------------------------------------
2.3 Warehouse-wise Delay Ranking
Purpose: Rank orders by delivery delay within each warehouse
--------------------------------------------------------- */
SELECT
Order_ID,
Warehouse_ID,
Route_ID,
GREATEST(
DATEDIFF(Actual_Delivery_Date, Expected_Delivery_Date),
0
) AS Delivery_Delay_Days,
RANK() OVER (
PARTITION BY Warehouse_ID
ORDER BY
GREATEST(
DATEDIFF(Actual_Delivery_Date, Expected_Delivery_Date),
0
) DESC
) AS Delay_Rank_In_Warehouse
FROM orders;
/*
Observation:
- Orders ranked based on delay within each warehouse
- Window function enables localized performance analysis
*/
/* =========================================================
TASK 3: Route Optimization Insights
========================================================= */
/* ---------------------------------------------------------
3.1 Route-Level Performance Metrics
Purpose:
- Calculate average delivery time
- Calculate average traffic delay
- Compute distance-to-time efficiency for each route
--------------------------------------------------------- */
SELECT
r.Route_ID,
r.Start_Location,
r.End_Location,
ROUND(
AVG(DATEDIFF(o.Actual_Delivery_Date, o.Order_Date)), 2
) AS Avg_Delivery_Time_Days,
r.Traffic_Delay_Min AS Avg_Traffic_Delay_Min,
ROUND(
r.Distance_KM / r.Average_Travel_Time_Min, 4
) AS Distance_Time_Efficiency
FROM routes r
JOIN orders o
ON r.Route_ID = o.Route_ID
GROUP BY
r.Route_ID,
r.Start_Location,
r.End_Location,
r.Distance_KM,
r.Average_Travel_Time_Min,
r.Traffic_Delay_Min;
/* ---------------------------------------------------------
3.2 Routes with Worst Efficiency Ratio
Purpose:
- Identify 3 least efficient routes based on
distance-to-time efficiency
--------------------------------------------------------- */
SELECT
Route_ID,
Start_Location,
End_Location,
ROUND(
Distance_KM / Average_Travel_Time_Min, 4
) AS Distance_Time_Efficiency
FROM routes
ORDER BY Distance_Time_Efficiency ASC
LIMIT 3;
/*
Observation:
- These routes take longer time per kilometer travelled
- Indicates congestion, poor infrastructure, or planning issues
- High potential for optimization
*/
/* ---------------------------------------------------------
3.3 Routes with More Than 20% Delayed Shipments
Purpose:
- Identify routes where delayed deliveries exceed 20%
--------------------------------------------------------- */
SELECT
Route_ID,
COUNT(*) AS Total_Orders,
SUM(
CASE
WHEN Delivery_Status = 'Delayed' THEN 1
ELSE 0
END
) AS Delayed_Orders,
ROUND(
(
SUM(
CASE
WHEN Delivery_Status = 'Delayed' THEN 1
ELSE 0
END
) * 100.0
) / COUNT(*),
2
) AS Delay_Percentage
FROM orders
GROUP BY Route_ID
HAVING Delay_Percentage > 20;
/*
Observation:
- Routes with more than 20% delayed orders identified
- Indicates consistent delivery reliability issues
- These routes require priority optimization
*/
/* ---------------------------------------------------------
Final Recommendation:
- Optimize routes with low efficiency ratios
- Re-evaluate transit time estimates for high-delay routes
- Consider alternate paths or delivery time windows
- Assign experienced delivery agents to critical routes
--------------------------------------------------------- */
-- =========================================================
-- TASK 4: WAREHOUSE PERFORMANCE
-- =========================================================
/* ---------------------------------------------------------
4.1 Top 3 Warehouses by Highest Average Processing Time
Purpose:
- Identify warehouses causing maximum internal delays
--------------------------------------------------------- */
SELECT
Warehouse_ID,
Location,
Processing_Time_Min
FROM warehouses
ORDER BY Processing_Time_Min DESC
LIMIT 3;
/*
Observation:
- Warehouses with highest processing times identified
- These locations are potential operational bottlenecks
- High processing time directly impacts delivery delays
*/
/* ---------------------------------------------------------
4.2 Total vs Delayed Shipments per Warehouse
Purpose:
- Compare shipment volume with delayed deliveries
--------------------------------------------------------- */
SELECT
Warehouse_ID,
COUNT(*) AS Total_Orders,
SUM(
CASE
WHEN Delivery_Status = 'Delayed' THEN 1
ELSE 0
END
) AS Delayed_Orders
FROM orders
GROUP BY Warehouse_ID;
/*
Observation:
- Shows workload handled by each warehouse
- Highlights warehouses contributing most to delayed orders
- Helps prioritize operational improvements
*/
/* ---------------------------------------------------------
4.3 Bottleneck Warehouses Using CTE
Purpose:
- Identify warehouses with processing time
greater than global average
--------------------------------------------------------- */
WITH Avg_Processing_Time AS (
SELECT
AVG(Processing_Time_Min) AS Global_Avg_Time
FROM warehouses
)
SELECT
w.Warehouse_ID,
w.Location,
w.Processing_Time_Min
FROM warehouses w
JOIN Avg_Processing_Time a
ON w.Processing_Time_Min > a.Global_Avg_Time;
/*
Observation:
- Warehouses exceeding global average processing time identified
- These are system-wide bottlenecks
- Improvement here yields maximum performance gains
*/
/* ---------------------------------------------------------
4.4 Rank Warehouses by On-Time Delivery Percentage
Purpose:
- Evaluate delivery reliability per warehouse
--------------------------------------------------------- */
SELECT
Warehouse_ID,
ROUND(
SUM(
CASE
WHEN Delivery_Status = 'On Time' THEN 1
ELSE 0
END
) * 100.0 / COUNT(*),
2
) AS On_Time_Delivery_Percentage,
RANK() OVER (
ORDER BY
SUM(
CASE
WHEN Delivery_Status = 'On Time' THEN 1
ELSE 0
END
) * 100.0 / COUNT(*) DESC
) AS Warehouse_Rank
FROM orders
GROUP BY Warehouse_ID;
/*
Observation:
- Warehouses ranked based on delivery reliability
- Higher rank indicates better on-time performance
- Supports data-driven warehouse benchmarking
*/
-- =========================================================
-- TASK 5: DELIVERY AGENT PERFORMANCE
-- =========================================================
/* ---------------------------------------------------------
5.1 Rank Delivery Agents per Route by On-Time Percentage
Purpose:
- Identify best and worst performing agents on each route
--------------------------------------------------------- */
SELECT
Agent_ID,
Route_ID,
On_Time_Percentage,
RANK() OVER (
PARTITION BY Route_ID
ORDER BY On_Time_Percentage DESC
) AS Agent_Rank_On_Route
FROM delivery_agents;
/*
Observation:
- Agents ranked within each route based on delivery reliability
- Rank 1 agents are the most reliable on their respective routes
- Enables route-level performance comparison
*/
/* ---------------------------------------------------------
5.2 Agents with On-Time Percentage Below 80%
Purpose:
- Identify underperforming delivery agents
--------------------------------------------------------- */
SELECT
Agent_ID,
Route_ID,
On_Time_Percentage
FROM delivery_agents
WHERE On_Time_Percentage < 80
ORDER BY On_Time_Percentage ASC;
/*
Observation:
- Agents with on-time delivery below 80% identified
- Indicates need for training or reassignment
- Helps improve overall delivery performance
*/
/* ---------------------------------------------------------
5.3 Compare Average Speed of Top 5 vs Bottom 5 Agents
Purpose:
- Evaluate speed difference between high and low performers
--------------------------------------------------------- */
SELECT
'Top 5 Agents' AS Agent_Group,
ROUND(AVG(Avg_Speed_KM_HR), 2) AS Avg_Speed
FROM (
SELECT Avg_Speed_KM_HR
FROM delivery_agents
ORDER BY On_Time_Percentage DESC
LIMIT 5
) AS Top_Agents
UNION ALL
SELECT
'Bottom 5 Agents' AS Agent_Group,
ROUND(AVG(Avg_Speed_KM_HR), 2) AS Avg_Speed
FROM (
SELECT Avg_Speed_KM_HR
FROM delivery_agents
ORDER BY On_Time_Percentage ASC
LIMIT 5
) AS Bottom_Agents;
/*
Observation:
- Top-performing agents have higher average speed
- Lower-speed agents correlate with poor on-time delivery
- Speed is a contributing factor to delivery performance
*/
-- =========================================================
-- TASK 6: SHIPMENT TRACKING ANALYTICS
-- =========================================================
/* ---------------------------------------------------------
6.1 Last Checkpoint and Time for Each Order
Purpose:
- Identify the most recent checkpoint reached by each order
--------------------------------------------------------- */
SELECT
Order_ID,
Checkpoint AS Last_Checkpoint,
Checkpoint_Time AS Last_Checkpoint_Time
FROM (
SELECT
Order_ID,
Checkpoint,
Checkpoint_Time,
ROW_NUMBER() OVER (
PARTITION BY Order_ID
ORDER BY Checkpoint_Time DESC
) AS rn
FROM shipment_tracking
) t
WHERE rn = 1;
/*
Observation:
- Retrieves the latest checkpoint reached by each order
- Helps track shipment progress and identify stalled deliveries
*/
/* ---------------------------------------------------------
6.2 Most Common Delay Reasons (Excluding 'None')
Purpose:
- Identify major causes of shipment delays
--------------------------------------------------------- */
SELECT
Delay_Reason,
COUNT(*) AS Occurrence_Count
FROM shipment_tracking
WHERE Delay_Reason <> 'None'
GROUP BY Delay_Reason
ORDER BY Occurrence_Count DESC;
/*
Observation:
- Traffic and Weather emerge as dominant delay reasons
- Enables targeted corrective actions to reduce delays
*/
/* ---------------------------------------------------------
6.3 Orders with More Than 2 Delayed Checkpoints
Purpose:
- Identify severely delayed shipments
--------------------------------------------------------- */
SELECT
Order_ID,
COUNT(*) AS Delayed_Checkpoint_Count
FROM shipment_tracking
WHERE Delay_Reason <> 'None'
GROUP BY Order_ID
HAVING Delayed_Checkpoint_Count > 2;
/*
Observation:
- Orders with repeated delays identified
- Indicates high-risk shipments requiring intervention
*/
-- =========================================================
-- TASK 7: ADVANCED KPI REPORTING
-- =========================================================
/* ---------------------------------------------------------
7.1 Average Delivery Delay per Region (Start_Location)
Purpose:
- Measure average delivery delay by route start location
--------------------------------------------------------- */
SELECT
r.Start_Location,
ROUND(
AVG(
DATEDIFF(o.Actual_Delivery_Date, o.Expected_Delivery_Date)
),
2
) AS Avg_Delivery_Delay_Days
FROM orders o
JOIN routes r
ON o.Route_ID = r.Route_ID
WHERE o.Actual_Delivery_Date > o.Expected_Delivery_Date
GROUP BY r.Start_Location;
/*
Observation:
- Shows regions contributing most to delivery delays
- Helps prioritize regional-level logistics improvements
*/
/* ---------------------------------------------------------
7.2 Overall On-Time Delivery Percentage
Purpose:
- Calculate delivery reliability KPI
--------------------------------------------------------- */
SELECT
ROUND(
SUM(
CASE
WHEN Delivery_Status = 'On Time' THEN 1
ELSE 0
END
) * 100.0 / COUNT(*),
2
) AS On_Time_Delivery_Percentage
FROM orders;
/*
Observation:
- Represents overall delivery reliability
- Higher percentage indicates better service performance
*/
/* ---------------------------------------------------------
7.3 Average Traffic Delay per Route
Purpose:
- Identify routes impacted by traffic congestion
--------------------------------------------------------- */
SELECT
Route_ID,
ROUND(AVG(Traffic_Delay_Min), 2) AS Avg_Traffic_Delay_Min
FROM routes
GROUP BY Route_ID;
/*
Observation:
- Highlights routes with consistently high traffic delays
- Supports route-level optimization decisions
*/
/* =========================================================
FINAL CONCLUSION & RECOMMENDATIONS
=========================================================
Key Findings:
- Several routes show consistently high traffic delays,
directly impacting delivery timelines.
- Overall on-time delivery performance indicates
significant scope for operational improvement.
- Certain warehouses act as processing bottlenecks,
increasing downstream delivery delays.
- Delivery agent performance varies notably across routes,
with speed strongly correlated to on-time delivery.
- Shipment tracking analysis highlights traffic, weather,
and sorting as primary delay drivers.
Recommendations:
- Prioritize optimization of routes with low efficiency
ratios and high traffic delays.
- Improve processing efficiency at bottleneck warehouses
through resource reallocation or process automation.
- Provide targeted training or route reassignment for
underperforming delivery agents.
- Introduce proactive delay mitigation strategies for
common delay reasons such as traffic and weather.
- Use these KPIs as a continuous monitoring framework
to track performance improvements over time.
Business Impact:
- Reduced delivery delays
- Improved customer satisfaction
- Lower operational and fuel costs
- Data-driven decision making for logistics optimization
========================================================= */