SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Using 4 tables
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

alain-janquart
Starting Member

Belgium
4 Posts

Posted - 10/01/2012 :  14:40:19  Show Profile  Reply with Quote
Hello,

I need to connect 4 tables.

Tabel 1:
P_ID, Firstname, lastname

Table 2:
P_ID, DOS_ID (P_ID can have multiple DOS_ID)

Table 3:
DOS_ID, test, test_date

Tabel 4:
DOS_ID, Payment, pay_date

I have made an sql-statement that display the dates from table 3 and table 4.Now I want to link these to the person from table 1.

Any idea how I can achieve this?

Thanks in advance

greetings

Lainkes

lazerath
Constraint Violating Yak Guru

USA
339 Posts

Posted - 10/01/2012 :  14:55:48  Show Profile  Reply with Quote
Can you provide what you have so far?
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 10/01/2012 :  14:57:28  Show Profile  Reply with Quote
The simple-minded way to join the tables would be as shown below - but that is probably not what you want. Is it possible that one DOS_ID have multiple tests and multiple payments? if that is the case, what do you need to get? Perhaps the number of tests done, or the total amount paid or something like that? Can you show with some sample data?
SELECT
	t1.P_ID,
	t1.FirstName,
	t1.Lastname,
	t2.P_ID,
	t2.DOS_PID,
	t3.DOS_ID,
	t3.test,
	t3.test_date,
	t4.DOS_ID,
	t4.Payment,
	t4.pay_date
FROM
	Table1 t1
	INNER JOIN Table2 t2 ON t1.P_ID=t2.P_ID
	LEFT JOIN Table3 t3 ON t3.DOS_ID=t2.DOS_ID
	LEFT JOIN Table4 t4 ON t4.DOS_ID = t2.DOS_ID;

Edited by - sunitabeck on 10/01/2012 14:58:11
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 10/01/2012 :  15:00:35  Show Profile  Reply with Quote
in any case posting some sample data and explaining your output will make it clear for us on what exactly you're looking at!

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

alain-janquart
Starting Member

Belgium
4 Posts

Posted - 10/01/2012 :  15:03:12  Show Profile  Reply with Quote
Every P_ID can have multiple DOS_ID's.
But one DOS_ID has only one record in table 3 and table 4.

What I have now is a list with following fields :
DOS_ID, test_date, pay_date, difference between those 2 days.

And I want :
Lastname, Firstname, DOS_ID, test_date, pay_date, difference between those 2 days.

Thanks

Lainkes
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 10/01/2012 :  15:05:32  Show Profile  Reply with Quote
quote:
Originally posted by alain-janquart

Every P_ID can have multiple DOS_ID's.
But one DOS_ID has only one record in table 3 and table 4.

What I have now is a list with following fields :
DOS_ID, test_date, pay_date, difference between those 2 days.

And I want :
Lastname, Firstname, DOS_ID, test_date, pay_date, difference between those 2 days.

Thanks

Lainkes



so for each P_ID whats rule for determining which DOS_ID to be returned? By default if you link using DOS_ID you will get mutiple records per PID. so you need to determine rule to fetch single DOS_ID per P_ID ie like earliest one, latest one,random one etc.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

alain-janquart
Starting Member

Belgium
4 Posts

Posted - 10/01/2012 :  15:10:17  Show Profile  Reply with Quote
I want to view all records, even if there are different ones for a P_ID

If P_ID with value 50 and name 'John Doe' has 3 DOS_ID's, then I want 3 rows like :
Doe, John, 50, test_date, pay_date, difference between those 2 days.
Doe, John, 50, test_date, pay_date, difference between those 2 days.
Doe, John, 50, test_date, pay_date, difference between those 2 days.

I hope I explain correctly what i want.

Greetings

Lainkes
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 10/01/2012 :  15:24:55  Show Profile  Reply with Quote
SELECT
	t1.Lastname,
	t1.FirstName,
	t2.DOS_PID,
	t3.test_date,
	t4.pay_date,
	DATEDIFF(dd,t3.test_date, t4.pay_date) AS Days
FROM
	Table1 t1
	INNER JOIN Table2 t2 ON t1.P_ID=t2.P_ID
	INNER JOIN Table3 t3 ON t3.DOS_ID=t2.DOS_ID
	INNER JOIN Table4 t4 ON t4.DOS_ID = t2.DOS_ID;
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 10/01/2012 :  15:28:25  Show Profile  Reply with Quote
quote:
Originally posted by alain-janquart

I want to view all records, even if there are different ones for a P_ID

If P_ID with value 50 and name 'John Doe' has 3 DOS_ID's, then I want 3 rows like :
Doe, John, 50, test_date, pay_date, difference between those 2 days.
Doe, John, 50, test_date, pay_date, difference between those 2 days.
Doe, John, 50, test_date, pay_date, difference between those 2 days.

I hope I explain correctly what i want.

Greetings

Lainkes


then Sunita's suggestion should work


SELECT
	t1.P_ID,
	t1.FirstName,
	t1.Lastname,
	t2.P_ID,
	t2.DOS_PID,
	t3.DOS_ID,
	t3.test,
	t3.test_date,
	t4.DOS_ID,
	t4.Payment,
	t4.pay_date,
        DATEDIFF(t3.test_date,t4.pay_date) AS TimeDiff
FROM
	Table1 t1
	INNER JOIN Table2 t2 ON t1.P_ID=t2.P_ID
	LEFT JOIN Table3 t3 ON t3.DOS_ID=t2.DOS_ID
	LEFT JOIN Table4 t4 ON t4.DOS_ID = t2.DOS_ID;



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

alain-janquart
Starting Member

Belgium
4 Posts

Posted - 10/01/2012 :  15:34:57  Show Profile  Reply with Quote
Thanks,

This seems to work.
Never had such a quick answer on whatever forum.
And a correct answer :-)

Thank you very much

Lainkes
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 10/01/2012 :  16:09:00  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.11 seconds. Powered By: Snitz Forums 2000