Solved

Combine three tables

Posted on 2013-06-19
5
251 Views
Last Modified: 2013-06-19
Hello:

I have three tables that look like this:

TableA

PersonNane
IDCodeAbbv
WorkID
Location
WorkDate

TableB
IDCodeAbbv
IDCodeName

TableC
WorkID
WorkLocation

In TableA if IDCodeAbbv is not null than WorkID will not be in TableC but IDCodeAbbv will be in TableB. If WorkID is not null than WorkID will be in TableC. At no time, will either IDCodeAbbv or WorkID be null at the same time.

This is an example of how the output should look like

PersonName        WorkID      WorkLocation          IDCodeAbbv               IDCodeName
Dan                          2                 Base 1                                                        
Tom                                                                             SR                        South Rear
John                        4                 Base 2
Sam                                                                             NE                        North Entrance
0
Comment
Question by:RecipeDan
  • 2
  • 2
5 Comments
 
LVL 9

Accepted Solution

by:
COANetwork earned 400 total points
ID: 39260607
SELECT
	A.PersonName,
	C.WorkID,
	C.WorkLocation,
	B.IDCodeAbbv,
	B.IDCodeName
FROM TableA A WITH(NOLOCK)
LEFT JOIN TableB B WITH(NOLOCK) ON A.IDCodeAbbv = B.IDCodeAbbv
LEFT JOIN TableC C WITH(NOLOCK) ON A.WorkID = C.WorkID

Open in new window

0
 
LVL 8

Assisted Solution

by:didnthaveaname
didnthaveaname earned 100 total points
ID: 39260626
I came up with the following (close to the same as COANetwork's):

select
	A.personName,
	coalesce( B.IDCodeAbbv, '' ) as IDCodeAbbv,
	coalesce( B.IDCodeName, '' ) as IDCodeName,
	coalesce( C.workID, '' ) as workID,
	coalesce( C.workLocation, '' ) as workLocation
from
	tableA as A
		left join tableB as B on A.IDCodeAbbv = B.IDCodeAbbv
		left join tableC as C on A.workID = C.workID;

Open in new window


Pretty much aesthetics, but you your sample output didn't have NULLs in it, so...
0
 
LVL 1

Author Comment

by:RecipeDan
ID: 39260909
Sorry about this...I did not create the database, it was handed to me.

After giviing the data a closer look. In TableA if IDCodeAbbv is not null than WorkID will not be in TableC but IDCodeAbbv will be in TableB. However, when IDCodeAbbv is not null WorkID is given a value of -1
0
 
LVL 9

Expert Comment

by:COANetwork
ID: 39260945
it does not matter, as long as there is no corresponding WorkID = -1 in TableC.  Both mine and didnthaveaname's sampels will work - mine will produce NULL where there is no data, his will produce an empty string.  The actual values of your keys do not matter, what matters is the presence or absence of SAME values in the corresponding tables.
0
 
LVL 1

Author Closing Comment

by:RecipeDan
ID: 39261116
Thank you both for your help
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

828 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