Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 joining tables

Author  Topic 

dmaxj
Posting Yak Master

174 Posts

Posted - 2010-06-25 : 23:14:22
Is there anyway to join these three tables:

Person
----------
pkPersonID
LastName
FirstName

Results
-----------
fkPersonID
Reading1
Reading2
Reading3
Reading4

Comments
-----------
fkPersonID
EventDate
Comments

I have been able to join the Person and Results tables. How can I join the Comments table so that I have one dataset? This is what I have so far:

SELECT *
FROM Person INNER JOIN Results ON
Person.pkPersonID = Results.fkPersonID

Regards

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-06-25 : 23:48:15
Try this:

SELECT *
FROM Person INNER JOIN Results ON
Person.pkPersonID = Results.fkPersonID
INNER JOIN Comments ON Person.pkPersonID= Comments.fkPersonID


Regards,
Bohra

I am here to learn from Masters and help new bees in learning.
Go to Top of Page

dmaxj
Posting Yak Master

174 Posts

Posted - 2010-06-26 : 00:35:45
I tried above code, but I am not getting desired result... I want to join Person with Results and Person with Comments.

Is this possible?

Regards
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-06-26 : 00:43:29
please post some sample data with expected result


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

dmaxj
Posting Yak Master

174 Posts

Posted - 2010-06-26 : 01:05:56
I am trying to get a dataset that contains:

pkPersonID, LastName, FirstName, Reading1, Reading2, Reading3, Reading4, EventDate, Comments

Regards
Go to Top of Page

dmaxj
Posting Yak Master

174 Posts

Posted - 2010-06-28 : 19:58:50
So it looks like more than one table cannot be joined to the same table? Given tables A, B, C:

Table A can be joined to B and then B to C....

Table A can joined to B and A joined to C - is this possible?

I considered using UNION, but the columns do not match in all tables. I also considered a subquery, but I am not sure if this could work. Any clues?

Regards
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-06-29 : 11:39:11
What you want for output doesn't seem clear to me. Bohra posted a query that seemed to be exactly what you asked for, but apparently is not what you wanted. We can help you better if you supply some sample data and expected output in a consumable format. Here is a link that might help you:
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Go to Top of Page
   

- Advertisement -