dbTalk Databases Forums  

Combining and Totaling 3 Tables?

comp.database.ms-sqlserver comp.database.ms-sqlserver


Discuss Combining and Totaling 3 Tables? in the comp.database.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
ww
 
Posts: n/a

Default 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

Any help would be appreciated.

WW

Reply With Quote
  #2  
Old   
scott.x.lu (Offline)
Junior Member
 
Posts: 5
Join Date: Jun 2006

Default 07-05-2006 , 03:00 AM






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

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.