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 2005 Forums
 Transact-SQL (2005)
 Problem With Joining Tables
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sql_monkey
Starting Member

United Kingdom
19 Posts

Posted - 06/07/2013 :  05:23:21  Show Profile  Reply with Quote
Hello,

I'm having trouble running this query against an insurance database:


SELECT DISTINCT c.Claimid, c.ClaimDate, n.Comments,
FROM dbo.tblClaim As c
JOIN dbo.tblNotes As n
ON c.Rowguid = n.Rowguid


If I join the tables using RIGHT JOIN then the Comments field is filled but I get a NULL in the ClaimID and ClaimDate field (which can't be right because all Claims have an ID and Date)

If I join the tables using LEFT JOIN the ClaimID and ClaimDate field are filled but there is a NULL in the Comments field.

If I use the WHERE clause like this:


SELECT DISTINCT c.Claimid, c.ClaimDate, n.Comments,
FROM dbo.tblClaim As c
JOIN dbo.tblNotes As n
ON c.Rowguid = n.Rowguid
WHERE c.ClaimID = '32961'


then the result just comes back blank, no error or NULL.

Any ideas where I'm going wrong here?

Thanks
Steven

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 06/07/2013 :  05:31:21  Show Profile  Reply with Quote
that means you dont have 1 to 1 relationship between the tables
ie there are claims existing without Notes
and there are Notes without claims.

If your attempt is to bring bothe information together regardless of match use this


SELECT DISTINCT c.Claimid, c.ClaimDate, n.Comments
FROM dbo.tblClaim As c
FULL JOIN dbo.tblNotes As n
ON c.Rowguid = n.Rowguid
AND  c.ClaimID = '32961'


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs

Edited by - visakh16 on 06/07/2013 05:31:38
Go to Top of Page

sql_monkey
Starting Member

United Kingdom
19 Posts

Posted - 06/07/2013 :  06:01:34  Show Profile  Reply with Quote
Thanks, that helps a lot.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 06/07/2013 :  06:17:51  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
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.06 seconds. Powered By: Snitz Forums 2000