I have two tables with the following structure:
CREATE TABLE COST1 (
ID,
COUNTER,
COST
)
CREATE TABLE COST2 (
ID,
COUNTER,
COST
)
ID can be used for a JOIN; and while COUNTER and COST are have the same name in both tables they are not related to each other the way ID is. I would like to create a result set COST3 that has the form:
ID, sum(COST1.cost) + sum(COST2.cost).
Here is what I have come up with but I don't know if summing over the original tables with a GROUP BY that results from the JOIN would work as I intend?
SELECT
ID,
( sum(c1.COST) + sum(c2.COST) ) as COST_TOTAL
FROM
COST1 c1
JOIN COST2 c2 ON c1.ID = c2.ID
GROUP BY
ID;
With some data, here is what the result should look like:
COST1
| ID | Counter | Cost |
|---|---|---|
| A | 1 | 50 |
| A | 2 | 30 |
| B | 1 | 25 |
| B | 2 | 30 |
COST2:
| ID | Counter | Cost |
|---|---|---|
| A | 1 | 20 |
| A | 2 | 40 |
| B | 1 | 50 |
| B | 2 | 10 |
| B | 3 | 20 |
COST3:
| ID | Cost |
|---|---|
| A, | 140 |
| B, | 135 |