I have a mySQL problem, where I need to calculate the SUM of rows after there is a mismatch in two relation tables. There are tests with numeric values and two part lists. Parts are usually included in both lists, and in this case all rows are summed up. Sometimes a part is only in the first list and not in the second, and in these cases all previous test values should be disregarded. In other words, rows should only be included in the sum if their test id is bigger than the last test id where this particular part was in list 1 but not in list 2.
There's a simplified mysql table listing at [url removed, login to view] where you can get an idea of the situation.
In this example, part 10 should sum all three test values, because the part is included in all lists, but part 20 should only return the value for test 3, as in test 2 it was not included in both in and out lists, and test 1 happened before test 2. So part 10 => 111 and part 20 => 100.
The job is to provide me with a MYSQL SELECT command, which returns the results specified above. Usage of complementary PHP and/or multiple queries is allowed, if the resulting code is faster.
I'll provide a more elaborate mysql dump to work with, so you can easily test your work.