Solved

Joining 2 data sets into 1

Posted on 2008-10-10
4
188 Views
Last Modified: 2013-11-29
I have the following senerio.

I have 2 tables that contain the following info.

Table1
Type      Parts
A      1
B      2
X      4

Table2
Type      Labor
A      4
B      5
C      6      

What I need is to output this.
Type      Parts      Labor
A      1      4
B      2      5      
C      0      6
X      4      0

I cannot simply use an outer join becuase there may be Types in one and not the other and vice versa (records C and X in this example).   I realize I could just create a Master [Type] list then outer join on it, but the example here is oversimplified since my key is actually 3 fields not just 1 like [Type] here.

I think I need a union query, but I cannot seem to get the following to work.

Select Type, Parts form Table1
union
Select Type, Labor from Table2

This returns Type and just parts no labor.



0
Comment
Question by:bhieb
  • 2
4 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 22687614
SELECT [Type], Sum(Parts) AS TotParts, 0 AS TotLabor
FROM Table1
GROUP BY [Type]
UNION
SELECT [Type], 0 AS TotParts, Sum(Labor) AS TotLabor
FROM Table2
GROUP BY [Type]
0
 

Author Comment

by:bhieb
ID: 22687721
That still only outputs TotParts.

Here is the actually query (there are 4 key fields not just Type).

Select Fleet,SITE, Reason, ReasCd, Desc, sum(LCost) as TotLabor  
FROM RO_Labor
Group By Fleet, SITE, Reason, ReasCd, [Desc]
Union
Select Fleet, SITE, Reason, ReasCd, Desc, sum(PCost) as TotPartts  
FROM RO_Parts
Group By Fleet, SITE, Reason, ReasCd, [Desc]

This is what it outputs.

Fleet      SITE      Reason      ReasCd      Desc      TotLabor
QT        VV          04      0        Not Applicable        327.2

Not column for Total Parts.
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
ID: 22688034
try if this will work for you

SELECT Table1.Type, Table1.Parts, Table2.Labor
FROM Table1 LEFT JOIN Table2 ON Table1.Type = Table2.Type
Union
SELECT Table2.Type, Table1.Parts, Table2.Labor
FROM Table2 LEFT JOIN Table1 ON Table2.Type = Table1.Type;
0
 

Author Comment

by:bhieb
ID: 22688436
Thanks cap that worked (technically), however I have 4 different data sets so combining all possible combinations woould will be a huge pain.  I think I will build a master table with all the keys then left join that to the 4 sub tables to get the totals.  Was just hoping that I could avoid that.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now