Search code examples
pythonmysqlcompareaddition

Python MySQL - consolidate (add up) values in certain column in table if values in other column are equal


I am trying to consolidate a MySQL table in python based on same values in one of the columns.

In the example below I am basically trying to sum-up the values in column "price" for all rows with equal values in column "id", while dropping column "unique_number".

Column "time" will always have the same values in all rows with the same values in column "id". Here it would be sufficient to either take the first occurance in "time" or perhaps calculate an average (would always be the same).

INPUT

enter image description here

OUTPUT

enter image description here

For example: the first two rows have the same value of "123" in column "id". Here I am trying to calculate the sum, which is 210.79. The "time" for both is the same (since the IDs are the same).

Can anyone help?

Thanks in advance


Solution

  • SELECT TIME, ID, SUM(VALUE)
    FROM TABLE
    GROUP BY TIME, ID