I'm trying to create a query whereby I SUM
a column but, if a column contains a certain value, the SUM
value has to be reset at that point to take on this value.
ie:
SUM(i.units * op.add_or_subtract) // This would translate to: '50 * -1' or '50 * 1'
The idea is that if op.op_code = 9
, the SUM
value should be reset to the current value of i.units
as a manual adjustment has taken place.
op_code | units | add_or_subtract | SUM value |
--------|-------|-----------------|------------
1 | 50 | 1 | 50
1 | 50 | 1 | 100
4 | 30 | -1 | 70
9 | 225 | 0 | 225
etc etc.
Can anyone help with how I could (if I can) achieve this in MySQL?