I have a table looks like this one below: +---------+---------+-------+ | SOURCE | DEST | VALUE | +---------+---------+-------+ | A | B | 1 | | B | A | 2 | | A | B | 3 | | C | D | 5 | | D | C | 6 | +---------+---------+-------+ So what I am trying to do is grouping them together by looking at the value1 and value2 without considering the order of this two values, which mean "A and B" is the same as "B and A". I am looking for a result looks like this: +---------+---------+-------------+-----------+ | | | SUM(SOURCE) | SUM(DEST) | +---------+---------+-------------+-----------+ | A | B | 4 | 2 | | C | D | 5 | 6 | +---------+---------+-------------+-----------+ Does anyone has any idea about how can I get this? (I am trying to do this on both SQL Server, and MySQL) Related: http://dba.stackexchange.com/questions/49252/grouping-rows-by-looking-at-two-columns-without-considering-the-order