Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
emyemyemy
Contributor III
Contributor III

Merge duplicate rows with same values across two columns in my mysql table and add the values in third column [duplicate]

I have a data  table with 3 columns like below:

Item1  Item2  Value

Apple   Orange  3

Apple  Banana  2

Mango  Apple  2

Banana  Apple  6

Apple   Mango   5

Orange  Banana   4

Orange   Apple   5

I want to remove the duplicate rows based on the combinations of two columns Item1 & Item2 and show only one in any order and want to add their values as a result, so as the final output in my table box  can be:

 

Item1   Item2   Value

Apple   Orange   8

Apple   Banana   8

Mango   Apple   7

Orange   Banana   4

 

Thank you for any help or attention !  or at least  can any one  answer me if its even possible to do that !! 

 

Labels (4)
1 Solution

Accepted Solutions
emyemyemy
Contributor III
Contributor III
Author

This is my solution : 

SELECT LEAST(Item1, Item2) AS Item1, GREATEST(Item1, Item2) AS Item2, SUM(Value) AS Value FROM yourTable GROUP BY 1, 2;

View solution in original post

1 Reply
emyemyemy
Contributor III
Contributor III
Author

This is my solution : 

SELECT LEAST(Item1, Item2) AS Item1, GREATEST(Item1, Item2) AS Item2, SUM(Value) AS Value FROM yourTable GROUP BY 1, 2;