1

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 !! 

Strawberry
  • 33,750
  • 13
  • 40
  • 57
yaacoub Amna
  • 47
  • 10

1 Answers1

1

You may use a LEAST/GREATEST trick here:

SELECT LEAST(Item1, Item2) AS Item1,
       GREATEST(Item1, Item2) AS Item2,
       SUM(Value) AS Value
FROM yourTable
GROUP BY 1, 2;
Item1 Item2 Value
Apple Orange 8
Apple Banana 8
Apple Mango 7
Banana Orange 4

Demo

Strawberry
  • 33,750
  • 13
  • 40
  • 57
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360