Solved

Joining 2 data sets into 1

Posted on 2008-10-10
4
192 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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