| Author |
Topic  |
|
|
alain-janquart
Starting Member
Belgium
4 Posts |
Posted - 10/01/2012 : 14:40:19
|
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
278 Posts |
Posted - 10/01/2012 : 14:55:48
|
| Can you provide what you have so far? |
 |
|
|
sunitabeck
Flowing Fount of Yak Knowledge
5152 Posts |
Posted - 10/01/2012 : 14:57:28
|
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 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47173 Posts |
Posted - 10/01/2012 : 15:00:35
|
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/
|
 |
|
|
alain-janquart
Starting Member
Belgium
4 Posts |
Posted - 10/01/2012 : 15:03:12
|
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
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47173 Posts |
Posted - 10/01/2012 : 15:05:32
|
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/
|
 |
|
|
alain-janquart
Starting Member
Belgium
4 Posts |
Posted - 10/01/2012 : 15:10:17
|
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 |
 |
|
|
sunitabeck
Flowing Fount of Yak Knowledge
5152 Posts |
Posted - 10/01/2012 : 15:24:55
|
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; |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47173 Posts |
Posted - 10/01/2012 : 15:28:25
|
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/
|
 |
|
|
alain-janquart
Starting Member
Belgium
4 Posts |
Posted - 10/01/2012 : 15:34:57
|
Thanks,
This seems to work. Never had such a quick answer on whatever forum. And a correct answer :-)
Thank you very much
Lainkes |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47173 Posts |
Posted - 10/01/2012 : 16:09:00
|
welcome
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
 |
|
| |
Topic  |
|
|
|