Q:

SQL Query to Group/SUM Columns C, D, when A, B are equal

I have a MySQL Table that has 4 columns, we will call them C1, C2, C3, and C4.
In this data, I have a data set like so:
C1,C2,C3,C4
A,B,12,24
C,D,23,13
A,D,19,15
B,C,15,15
A,C,21,9
B,D,19,2
B,A,8,19
D,C,29,1

I need to get an output like follows:
A,71
B,66
D,59
C,48

I attempted to write a query, but just can't seem to wrap my head around how to Sum two fields when two other fields are equal and group/sort them.
SELECT C1, SUM(C3,C4) AS Total
WHERE C1 = C2
GROUP BY C1
ORDER BY C1 Desc;

This returns a null set and I know why, becase C1 and C2 are not equal in the same row, but can't seem to figure out what to do next.

A:

I would do this as:
select col, sum(val)
from ((select c1 as col, c3 as val from t
      ) union all
      (select c2, c4 from t
      )
     ) cc
group by col;

This seems like the simplest approach given the data you have provided.  This should have much better performance than an approach that uses or in a join condition.

