I have two tables with the same attributes in MySQL. I want the difference between two columns but if the difference is equal to 0 the rest value of one of the columns has to calculate for the next row.
For example:
P1, K1, 5, 2017-06-31; P2, K1, 6, 2017-05-21; for the first table and
P3, K1, 3, 2017-07-03; P4, K1, 7, 2017-06-21; for the second table.
The result must be:
P1, K1, 0 AND P2, K1, 1.
This because we have 5 for P1 and K1 that are input and 7 of P4 and K1 that are output with the FIFO logic. So 5 - 7 = 0 and the rest is 2. The next date after the first is P2 AND K1 with 6. The rest from the first was 2 so 6 - 2 = 4 so we have to look at the second date of output that is P3 and K1 with 3 value so 4 - 3 = 1. In conclusion the result is P1, K1, 0 AND P2, K1, 1.
Please help.