Solved

Joining 2 data sets into 1

Posted on 2008-10-10
4
190 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 120

Accepted Solution

by:
Rey Obrero (Capricorn1) 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Dlookup MSACCESS 5 25
Query Syntax 17 36
update all email addresses SQL 1 24
Trying to force an answer in a combobox 7 18
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

777 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