I have a MySQL table describing the maintenance cycle of railway vehicles (type of overhaul and period in kilometers):
CREATE TABLE `cycle_test` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`type` varchar(2) COLLATE utf8_spanish_ci NOT NULL,
`km` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci
I have populated this table as follows:
INSERT INTO `cycle_test` (`id`, `type`, `km`) VALUES (NULL, 'R1', '12000'), (NULL, 'R2', '24000'), (NULL, 'R3', '72000'), (NULL, 'R4', '144000');
SELECT type, km FROM cycle_test;
+------+--------+
| type | km |
+------+--------+
| R1 | 12000 |
| R2 | 24000 |
| R3 | 72000 |
| R4 | 144000 |
+------+--------+
The goal is to obtain the maintenance schedule, with all the overhauls to be carried out throughout the cycle, and the mileage of each overhaul, taking into account that, when overhauls of different types coincide, the one with the highest rank (R4 > R3 > R2 > R1) supersedes the ones with the lowest rank, as shown below:
+--------+------+
| km | type |
+--------+------+
| 12000 | R1 |
| 24000 | R2 |
| 36000 | R1 |
| 48000 | R2 |
| 60000 | R1 |
| 72000 | R3 |
| 84000 | R1 |
| 96000 | R2 |
| 108000 | R1 |
| 120000 | R2 |
| 132000 | R1 |
| 144000 | R4 |
+--------+------+
this problem I have already solved in PHP, but I am looking for a purely MySQL solution, either with session variables or stored procedures. The (possible) solution must be scalable for different maintenance cycles (not hardcoded). Thanks in advance for your help and/or hints.
Well, with the help of the MySQL docs, it has turned out easier than I thought. First, I have created the schedule table (in production, it will be a temporary table):
The
UNIQUE KEYkmallows detecting the concurrence of overhauls of different ranks with the same mileage.Then I have created the following stored procedure in the database:
And this is the result I wanted, after running the procedure: