SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


t-sql joins


t-sql joins

Author
Message
inevercheckthis2002
inevercheckthis2002
SSC Eights!
SSC Eights! (863 reputation)SSC Eights! (863 reputation)SSC Eights! (863 reputation)SSC Eights! (863 reputation)SSC Eights! (863 reputation)SSC Eights! (863 reputation)SSC Eights! (863 reputation)SSC Eights! (863 reputation)

Group: General Forum Members
Points: 863 Visits: 485
I have three tables.

TableA
id_number
date
name

TableB
id_number
notes
status

TableC
id_number
location
item

Sometimes data may be entered in TableA and TableB.
Sometimes data may be entered in TableA and TableC.
Sometimes data may be entered in TableA and TableB and Table C..

I would like to return:
A.id_number B.notes B.status C.location C.item

..leaving fields from B or C empty when there is no data from them.

So, I have:

SELECT A.id_number B.notes B.status
FROM TableA as A
INNER JOIN TableB as B
ON A.id_number = B.id_number

...and I have
SELECT A.id_number C.location C.item
FROM TableA as A
INNER JOIN TableC as C
ON A.id_number = C.id_number


...how do I then combine the two so that I get
A.id_number B.notes B.status C.location C.item


Thanks....
Jan Van der Eecken
Jan Van der Eecken
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5163 Visits: 6543
Just OUTER JOIN tableA, tableB and tableC on id_number.

--------------------------------------------------------------------------
A little knowledge is a dangerous thing (Alexander Pope)
In order for us to help you as efficiently as possible, please read this before posting (courtesy of Jeff Moden)
Sean Lange
Sean Lange
SSC Guru
SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)

Group: General Forum Members
Points: 149265 Visits: 18575
Like this:


SELECT A.id_number, B.notes, B.status, C.location, C.item
FROM TableA as A
LEFT JOIN TableB as B ON A.id_number = B.id_number
LEFT JOIN TableC as C ON A.id_number = C.id_number



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
inevercheckthis2002
inevercheckthis2002
SSC Eights!
SSC Eights! (863 reputation)SSC Eights! (863 reputation)SSC Eights! (863 reputation)SSC Eights! (863 reputation)SSC Eights! (863 reputation)SSC Eights! (863 reputation)SSC Eights! (863 reputation)SSC Eights! (863 reputation)

Group: General Forum Members
Points: 863 Visits: 485
Jan Van der Eecken (6/19/2013)
Just OUTER JOIN tableA, tableB and tableC on id_number.


Point of clarification -- sometimes data will be entered in TableA and not in TableB or TableC. We do not want the rows from TableA unless there is matching data in TableB and/or TableC.

My understanding is that an outer join would return all rows from TableA.
Kingston Dhasian
Kingston Dhasian
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11407 Visits: 5331
inevercheckthis2002 (6/20/2013)
Jan Van der Eecken (6/19/2013)
Just OUTER JOIN tableA, tableB and tableC on id_number.


Point of clarification -- sometimes data will be entered in TableA and not in TableB or TableC. We do not want the rows from TableA unless there is matching data in TableB and/or TableC.

My understanding is that an outer join would return all rows from TableA.


Quite simple. Add a WHERE clause to the query provided by Sean.

SELECT A.id_number, B.notes, B.status, C.location, C.item
FROM TableA as A
LEFT JOIN TableB as B ON A.id_number = B.id_number
LEFT JOIN TableC as C ON A.id_number = C.id_number
WHERE B.is_number IS NOT NULL OR C.id_number IS NOT NULL




Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search