Is there a way to combine (SUM) rows with unique ID (column) numbers by specifying the ID numbers in a list and then make MYSQL combine rows in a table according to the specification in the list?
The data in the database I use change frequently changed but the customer ID that needs to be combined remains the same so it would be great if there was a a way to have the list in a separate file/table or similar to read form when I re-create the database with new values.
For example I want to combine ID 112 and 115 when I recreate the database to ID 112 with value 1 equal to 9 and value 2 equal to 12 (the sum of 112 and 115).
ID | VALUE 1 | VALUE 2
------------------------
...
112| 2 |5
113| 5 |6
114| 6 |6
115| 7 |7
...
To new table:
ID | VALUE 1 | VALUE 2
------------------------
...
112| 9 |12
113| 5 |6
114| 6 |6
...
This is sort of what I am looking for but is there a way to read the ID's from a list with several values? For example ( 112,115), ( 449,622), ( 123,423,321) etc.?
Something like:
select min(id) as newId,
sum(value_1) as newValue1,
sum(value_2) as newValue2
from myTable t
where id in list;
How would the list need to be formatted?
Best Answer
The following query should give you
112, 9, 12