Ivy Professional School
Rating
Learning/SQL Data Drill #14

Vehicle Capacity Allocation

Last updated

Find the list of packages each vehicle can carry without exceeding its fixed capacity.

SQLIntermediateWindow FunctionsRunning TotalSUM OVERGROUP_CONCATCTECapacity Analysis

Your Objective

Find the list of packages each vehicle can carry without exceeding its fixed capacity.

The Setup

Your dataset contains two logistics tables: 1. A Vehicle table with the vehicle ID and maximum carrying capacity 2. A Packages table with each package ID, package weight, and the vehicle assigned to carry it Analyze whether assigned packages can be loaded into each vehicle without crossing the vehicle's capacity.

Table Schema

Vehicle
ColumnTypeKey / Notes
IDINTPK
CapacityINT
Packages
ColumnTypeKey / Notes
IDINTPK
WeightINT
vehicle_IDINTFK

Sample Data

Vehicle — 4 rows

IDCapacity
1550
2650
3500
4600

Packages — 30 rows

IDWeightvehicle_ID
1551
2601
3652
4702
5753
6803
7854
8904
9951
101002
11523
12574
13621
14672
15723
16774
17821
18872
19923
20974
21541
22592
23643
24694
25741
26792
27843
28894
29941
30992

Your Task

Vehicle Capacity Allocation: What is the comma-separated list of packages each vehicle can carry without exceeding its capacity? The packages should be selected in ascending order of their weight. The final result table should have vehicle_id and packages columns.

Your output should look like this (partial example):

vehicle_idpackages
121,1,2,13,25,17,29
222,3,14,4,26,18,30
311,23,15,5,6,27

Before You Solve

  • →Join Packages with Vehicle so every package row also has its assigned vehicle capacity.
  • →Use SUM(Weight) OVER (PARTITION BY vehicle_ID ORDER BY Weight) to calculate a running total per vehicle.
  • →Filter rows where the running total is less than or equal to the vehicle capacity.
  • →Use GROUP_CONCAT to combine the selected package IDs into one comma-separated list per vehicle.
Explore Enterprise Solutions