I have a 4 column table, the primary key is a composite of id, gameId and flag.
id | gameId | score | flag
--------------------------
1 | 1 | 10 | 1
1 | 1 | 20 | 0
2 | 1 | 1 | 0
1 | 2 | 10 | 1
3 | 2 | 1 | 0
I need to update the table so that:
All of gameId 2 scores are added to gameId 1 where the id and flag is the same. (e.g. row 1 would have a score of 20 by adding the scores of row 1 and row 4)
If the above happens the gameId 2 row needs to be deleted.
Where the above isn't found (e.g. there is a row where the gameId is 2 but the id and flag don't match another row), the gameId can just be changed to 1.
So my table after the SQL is complete should look like this:
id | gameId | score | flag
--------------------------
1 | 1 | 20 | 1
1 | 1 | 20 | 0
2 | 1 | 1 | 0
3 | 1 | 1 | 0
How can I write this in SQL? Thanks :)