I have two tables that are something like this..
table2015 table2016
=======+===========+======== =======+===========+========
id |remains |days_off id |remains |days_off
=======+===========+======== =======+===========+========
1 |4 |12 1 |12 |12
2 |-2 |12 2 |12 |12
3 |3 |25 3 |25 |25
How can I edit days_off on table2016 only if the remains on table2015 are less than 0? This is the result needed:
table2015 table2016
id |remains |days_off id |remains |days_off
=======+===========+======== =======+===========+========
1 |4 |12 1 |12 |12
2 |-2 |12 2 |12 |10
3 |0 |25 3 |25 |25
I've tried this query but it gave me an error on line 5
UPDATE
table2016
SET
table2016.days_off = table2016.days_off + table2015.remains
FROM
table2016
JOIN
table2015
ON
table2016.id = table2015.id
AND
table2015.remains < 0