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 2008 Forums
 Transact-SQL (2008)
 LEFT JOIN not returning all rows from right table

Author  Topic 

papaboom
Starting Member

1 Post

Posted - 2011-08-11 : 16:42:37
Not sure what I'm doing wrong here - if someone could set me straight I would be grateful.

SELECT r.Idx, r.valuerange, ISNULL(COUNT(s.[Status]),0) as valuecount \FROM
DistillationBy25K as r
LEFT OUTER JOIN Status as s ON s.listingprice < r.RangeMax AND s.listingprice >= r.rangeminINNER JOIN mls_data as m ON m.listingnumber = s.listingnumber AND m.mlssource = s.mlssource

WHERE

(m.county like isnull(@County, m.county) + '%') and (m.mlssource like isnull(@Region, m.mlssource) + '%') and (m.schooldistrict like isnull(@Schooldistrict, m.schooldistrict) + '%') and (m.zip like isnull(@Zip, m.zip) + '%') and dateaddedmonth = month(@DateTo) and dateaddedyear = year(@DateTo) and s.status <> 'Sold'

GROUP BY r.Idx, r.valuerange

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2011-08-11 : 16:56:04
You have, effectivly, created an INNER JOIN by adding "s.status <> 'Sold'" to your WHERE clause. Perhaps you want to move that perdicate to the join condition instead? If that doesn't work we'll probably need more information. Here is a link that can help you prove that information in a consumable format for us:
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-08-12 : 02:05:13
see also
http://weblogs.sqlteam.com/jeffs/archive/2007/05/14/criteria-on-outer-joined-tables.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -