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)
 LEFT OUTER JOIN incorrect # of records

Author  Topic 

slb
Starting Member

4 Posts

Posted - 2008-10-15 : 11:47:00
This should be simple but my numbers are off. I have two tables, TableA has 2161 records where field ttype = 9 and in TableB I have 4766 records where ttype = 9 however I do not want to join them on this field but rather on the custID field. So I want to get all the records that are in TableA where ttype = 9 and the records from TableB where ttype = 9 and the custID in both tables match so this is my query:

SELECT * FROM TABLEA LEFT OUTER JOIN TABLEB ON
TABLEA.CUSTID = TABLEB.CUSTID
WHERE TABLEA.TTYPE = 9 AND TABLEB.TTYPE = 9

So when I run this query the total number of records is 1854 but shouldn't it be 2161 since I'm getting all the records from TableA??
What am I doing wrong?

I've also tried

SELECT * FROM TABLEA LEFT OUTER JOIN TABLEB ON
TABLEA.CUSTID = TABLEB.CUSTID
WHERE TABLEA.TTYPE = 9 AND TABLEB.TTYPE = 9

Which returns 3913 rows ????

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-10-15 : 11:48:45
[code]SELECT *
FROM TABLEA
LEFT JOIN TABLEB ON TABLEB.CUSTID = TABLEA.CUSTID
AND TABLEB.TTYPE = 9
WHERE TABLEA.TTYPE = 9[/code]


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-15 : 11:49:58
quote:
Originally posted by slb

This should be simple but my numbers are off. I have two tables, TableA has 2161 records where field ttype = 9 and in TableB I have 4766 records where ttype = 9 however I do not want to join them on this field but rather on the custID field. So I want to get all the records that are in TableA where ttype = 9 and the records from TableB where ttype = 9 and the custID in both tables match so this is my query:

SELECT * FROM TABLEA LEFT OUTER JOIN TABLEB ON
TABLEA.CUSTID = TABLEB.CUSTID
AND TABLEB.TTYPE = 9
WHERE TABLEA.TTYPE = 9

So when I run this query the total number of records is 1854 but shouldn't it be 2161 since I'm getting all the records from TableA??
What am I doing wrong?

I've also tried

SELECT * FROM TABLEA LEFT OUTER JOIN TABLEB ON
TABLEA.CUSTID = TABLEB.CUSTID
WHERE TABLEA.TTYPE = 9 AND TABLEB.TTYPE = 9

Which returns 3913 rows ????




modify like above
Go to Top of Page

slb
Starting Member

4 Posts

Posted - 2008-10-15 : 12:15:19
I changed the query but now it returns 2492 records not the 2161 I'm assuming it should return?
Go to Top of Page

slb
Starting Member

4 Posts

Posted - 2008-10-15 : 12:20:51
Never mind - tableB was displaying Null values which I don't want so changed the query to

SELECT *
FROM TABLEA
LEFT JOIN TABLEB ON TABLEB.CUSTID = TABLEA.CUSTID
AND TABLEB.TTYPE = 9 AND TABLEB.TTYPE <> NULL
WHERE TABLEA.TTYPE = 9

and I got the right number of records!! THANK YOU!!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-15 : 12:43:30
quote:
Originally posted by slb

Never mind - tableB was displaying Null values which I don't want so changed the query to

SELECT *
FROM TABLEA
LEFT JOIN TABLEB ON TABLEB.CUSTID = TABLEA.CUSTID
AND TABLEB.TTYPE = 9 AND TABLEB.TTYPE <> NULL
WHERE TABLEA.TTYPE = 9

and I got the right number of records!! THANK YOU!!


Are you having ANSI NULLS set to OFF? else use TABLEB.TTYPE IS NOT NULL instead of TABLEB.TTYPE <> NULL
Go to Top of Page

raky
Aged Yak Warrior

767 Posts

Posted - 2008-10-15 : 12:49:31
quote:
Originally posted by slb

Never mind - tableB was displaying Null values which I don't want so changed the query to

SELECT *
FROM TABLEA
LEFT JOIN TABLEB ON TABLEB.CUSTID = TABLEA.CUSTID
AND TABLEB.TTYPE = 9 AND TABLEB.TTYPE <> NULL
WHERE TABLEA.TTYPE = 9

and I got the right number of records!! THANK YOU!!



I think there is no need to check the condition TABLEB.TTYPE <> NULL in left outer join because you already set the TABLEB.TTYPE = 9 condition in left join..
Go to Top of Page
   

- Advertisement -