The actual problem is about optimizing a cache database, I’ve re-phrased it into a simple scenario:
A car dealer wants to get rid of old cars, and lists them for sale in a table:
| Car_Number | Car_Year | Car_Price |
|------------|-----------|-----------|
|1001........|1967.......|29000......|
|1002........|1930.......|29050......|
|1003........|2001.......|30000......|
|1004........|1980.......|10000......|
|1005........|1967.......|75000......|
|1006........|2005.......|80000......|
|1007........|1995.......|21000......|
|1008........|1920.......|55000......|
A customer wants to select maximum number of cars within a fixed budget, With older cars preferred over newer ones, or the cars with older number in case two cars have same year.
The only way I can currently think about this problem is obtaining a sorted view and manually adding up prices in a loop until the sum hits budget limit. Then, return a list of numbers of cars added.
what would be the minimal set of SQL commands required for above scenario ?
Find all cars that don’t break the budget: