all groups > sql server programming > february 2005 >
You're in the

sql server programming

group:

Joins Question


Joins Question agarwalp NO[at]SPAM eeism.com
2/1/2005 11:34:01 PM
sql server programming: I have 3 tables. Table1(time,readingA) Table2(time,readingB)
table3(time,readingC)
Now the time can be same and it can be different. Now i want to know
how do i join so that i get the data: time,readingA,readingB,readingC

If the time is same then it is fine, but if the time is not same in
two tables for eg : if table1 has a record for time 12:30 and Table2
and table3 does not have that time then it should show data from
table1 and the readingB and readingC will be blank.


I hope my question is clear.

RE: Joins Question John Bell
2/2/2005 12:19:03 AM
Hi

If you are not interested in the time then you may not want to truncate
everything to midnight when they are inserted (which if you don't have a time
portion on your date/time will happen anyhow). The isssue then is what will
happen if there are multiple records for each day? If the time is require for
some other reason you can also use the convert function to compare the date
part of the datetime

SELECT CONVERT(char(8),T1.time,112) AS Time, T1.readingA, T2.readingB,
T3.readingC
FROM Table1 T1
JOIN Table2 T2 ON CONVERT(char(8),T1.time,112) = CONVERT(char(8),T2.time,112)
JOIN Table3 T3 ON CONVERT(char(8),T1.time,112) = CONVERT(char(8),T3.time,112)

John

[quoted text, click to view]
Re: Joins Question John Bell
2/2/2005 5:17:46 AM
It looks like I may have got this mixed up! As Ana says use left JOIN
although you may not want your times to 3/100 of a second, in which
case you will still need to truncate them

SELECT T1.Time, T1.readingA, T2=AD.readingB,
T3.readingC
FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.time =3D T2.time
LEFT JOIN Table3 T2 ON T1.time =3D T3.time


John
Re: Joins Question Ana Mihalj
2/2/2005 9:38:10 AM
Hi,

if I understand correctly your question, you need to use left join.
Something like



SELECT T1.time AS Time, T1.readingA, T2.readingB,
T3.readingC
FROM Table1 T1
LEFT JOIN Table2 T2

ON T2.time=T2.time
LEFT JOIN Table3 T3

ON T1.time= T3.time





[quoted text, click to view]

AddThis Social Bookmark Button