I have two tables set up basically like this (extremely lite version):
Table 1:
ID Amt 1 Amt 2
-------------------------
112 $20 $30
112 $50 $60
125 $75 $05
Table 2:
ID Amt 3 Amt 4
-------------------------
112 $25 $30
125 $40 $60
125 $110 $120
All the amount columns needed to be summed separately, the output to be similar to this:
Query results:
ID Amt 1 Amt 2 Amt 3 Amt 4
--------------------------------------------
112 $70 $90 $25 $30
125 $75 $05 $150 $180
The joins I’ve tried result in the records duplicating (and summing) by a factor of the times the ID repeats in the second table. I have no primary key to connect these tables.
Any help would be greatly appreciated.
Thanks!
On SQL Server 2005 or newer, you could use two CTE’s (Common Table Expression) to do the summing/grouping per table, and then join the two – something like this:
This gives me your desired output