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)
 Problem With Joining Tables

Author  Topic 

sql_monkey
Starting Member

19 Posts

Posted - 2013-06-07 : 05:23:21
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

52326 Posts

Posted - 2013-06-07 : 05:31:21
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
Go to Top of Page

sql_monkey
Starting Member

19 Posts

Posted - 2013-06-07 : 06:01:34
Thanks, that helps a lot.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-07 : 06:17:51
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -