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 2005 Forums
 Transact-SQL (2005)
 SELECT help

Author  Topic 

jsmoritz
Starting Member

3 Posts

Posted - 2009-02-13 : 17:04:22
I have a select statement that reads:

SELECT tblStudent.strStuLastName, tblBank.intBankBalance
FROM tblStudent INNER JOIN
tblBank ON tblStudent.StudentID = tblBank.intBankStuID
WHERE tblStudent.booStuInactive = 0

This is my question. Some students are not in tblBank because they haven't received any points yet but I want them included in the query as having a balance of 0. Is there a way to do this?

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-02-13 : 17:06:49
[code]SELECT tblStudent.strStuLastName, Coalesce(tblBank.intBankBalance,0)as IntBankBalance
FROM tblStudent LEFT OUTER JOIN
tblBank ON tblStudent.StudentID = tblBank.intBankStuID
WHERE tblStudent.booStuInactive = 0
[/code]
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-02-13 : 17:07:04
You need to use an OUTER JOIN then.

SELECT tblStudent.strStuLastName, tblBank.intBankBalance
FROM tblStudent
LEFT OUTER JOIN tblBank ON tblStudent.StudentID = tblBank.intBankStuID
WHERE tblStudent.booStuInactive = 0

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-02-13 : 17:07:19


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-02-13 : 17:10:40
quote:
Originally posted by sodeep

SELECT tblStudent.strStuLastName, Coalesce(tblBank.intBankBalance,0)as IntBankBalance
FROM tblStudent LEFT OUTER JOIN
tblBank ON tblStudent.StudentID = tblBank.intBankStuID
WHERE tblStudent.booStuInactive = 0




Sorry,Should be:

SELECT tblStudent.strStuLastName, Coalesce(tblBank.intBankBalance,0)as IntBankBalance
FROM tblStudent LEFT OUTER JOIN
tblBank ON tblStudent.StudentID = tblBank.intBankStuID
AND tblStudent.booStuInactive = 0
Go to Top of Page

jsmoritz
Starting Member

3 Posts

Posted - 2009-02-13 : 17:17:53
Thank you, thank you, thank you!!!!!
Go to Top of Page

jsmoritz
Starting Member

3 Posts

Posted - 2009-02-13 : 17:39:44
It worked great. I have never understood outer joins and now I do. Coalesce is new to me also and will be handy in the future. The statement works great. Thanks again.
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-02-13 : 17:45:22
You are Welcome.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-14 : 01:06:25
quote:
Originally posted by jsmoritz

It worked great. I have never understood outer joins and now I do. Coalesce is new to me also and will be handy in the future. The statement works great. Thanks again.


read this for more info

http://weblogs.sqlteam.com/jeffs/archive/2007/05/14/60205.aspx

http://sqlblogcasts.com/blogs/madhivanan/archive/2007/10/04/isnull-or-coalesce.aspx
Go to Top of Page
   

- Advertisement -