I have a table structure similar to this:
id | order1_id | order1_type | order1_amount | order2_id | order2_type | order2_amount
------------------------------------------------------------------------------------------
1 1 3 4 1 4 5
2 2 1 1 1 3 2
3 1 4 4 2 2 1
I want to get the data like this:
order_id | order_type | order_amount
1 3 6
1 4 9
2 1 1
2 2 1
I want to group by type, and sum the order amounts. How can I do that ?
Thanks,
I’m going to use a union in a subquery to line the columns up, then group and sum on that.
Assuming you’re stuck with this less-than-ideal table structure, you may want to create a view that represents the subquery below, then run the group by/sum against that view. I’m guessing such a view might be useful in more places than just this one query.