Combining and Totaling 3 Tables? -
02-03-2005
, 06:07 PM
Can someone please tell me how to do the following:
I have three tables, each table has the same fields.
Each table has an "ItemNumber" field but not necessarily the same
items.
I want to find each 'ItemNumber' in table2 and table3 that matches
each 'ItemNumber' from table1. However, I want to include in my result
all 'ItemNumber' from table1, table2 and table3. I would also like to
total each item. I realize I may have to do this in multiple queries
but I don't know where to begin.
For example:
Table1
ItemNumber Jan Feb
001 1 1
002 1 1
003 1 1
table2
ItemNumber Jan Feb
001 1 1
002 1 1
004 1 1
table3
ItemNumber Jan Feb
002 1 1
003 1 1
004 1 1
ResultTable Jan Feb
001 2 2
002 3 3
003 2 2
004 2 2
SELECT ItemNumber, SUM(Jan) as Jan, SUM(Feb) as Feb FROM
(
SELECT ItemNumber,Jan, Feb from Table1
UNION All
SELECT ItemNumber,Jan, Feb from Table2
UNION All
SELECT ItemNumber,Jan, Feb from Table3
) A
GROUP BY A.ItemNumber