I have some similar selects that I'd like to join and aggregate values.
I've already tried with join, inner join, union, union all, without the expected result.
See that the selects are very similar. The differences are:
SUM(comumn_02)*.05 AS SUM_01 different from SUM(comumn_02)*.45 AS SUM_02
and AND COLUMN_05 = ALFA different from AND COLUMN_05 = BETA in the second select.
The whole thing is here:
SELECT
COLUMN_01,
SUM(COLUMN_02)*.05 AS SUM_01
FROM table
WHERE
COLUMN_03 = 1
AND COLUMN_04 = 2
AND COLUMN_05 = ALFA
GROUP BY COLUMN_01
UNION ALL
SELECT
COLUMN_01,
SUM(COLUMN_02)*.45 AS SUM_02
FROM table
WHERE
COLUMN_03 = 1
AND COLUMN_04 = 2
AND COLUMN_05 = BETA
GROUP BY COLUMN_01;
In this example we have the following result:
|-----------|--------|
| COLUMN_01 | SUM_01 |
|-----------|--------|
| value_01 | 465 |
| value_02 | 186 |
| value_03 | 245 |
| value_01 | 102 |
| value_02 | 108 |
| value_03 | 325 |
|--------------------|
But what I'd like to have would be:
|-----------|--------|
| COLUMN_01 | SUM_01 |
|-----------|--------|
| value_01 | 567 | //sum of 465 + 102
| value_02 | 294 | //sum of 186 + 108
| value_03 | 570 | //sum of 245 + 570
|--------------------|
And most important, would this be scalable? i.e. to use it with three or more select "unions" or the performance would decrease a lot?