sql sum duplicates in multiple columns

后端 未结 1 1675
执念已碎
执念已碎 2021-01-29 15:42

I\'m trying to sum values from duplicates rows (with the same ID, Month and Person) in multiple columns to the first or the last duplicate row. Then delete the duplicate rows ex

相关标签:
1条回答
  • 2021-01-29 16:01
    SELECT ID, Month, Person, SUM(Value1) as SumValue1, SUM(Value2) AS SumValue2
    FROM db.Hours
    GROUP BY ID, Month, Person
    

    I am not sure why you are looking at this as two steps etc. There is no removal of duplicates etc. this is a scenario for Group By Aggregation. Where you group like rows and summarize the value columns. The only reason you would need to make this a multi step operation would be if one of your value columns will be considered within your grouping e.g. ID, Month, Person, and Value1. In your case you simply need to group by ID, Month, Person and do the aggregation for Value1 and Value2.

    0 讨论(0)
提交回复
热议问题