Solved

Combine three tables

Posted on 2013-06-19
5
249 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
get column names from table in vb.net 8 28
Sql server function help 15 29
SQL SELECT query help 7 37
optimize stored procedure 6 25
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

770 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