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 2008 Forums
 Transact-SQL (2008)
 left join not displaying all records
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

kcarbone1970
Starting Member

USA
31 Posts

Posted - 02/25/2014 :  02:13:09  Show Profile  Reply with Quote
Hello I have this query that should produce about 600 records (for each student) and it works fine until I add an additional filter in the where clause by which reduces the number of records to about half. I need the condition be applied to display all the records. Any suggestions greatly appreciated. The additional filter is (a.DT >= '2013-09-01')


DECLARE @SC INT = 30, @GR INT = 9;
 
   SELECT  
s.FN + ' ' + LN AS NAME ,
s.ID ,
        s.GR ,
        s.SC ,
        
        ( CASE WHEN s.RC1 = 700
                    AND ETH = 'Y' THEN 'Hispanic'
               WHEN s.RC1 = 600
                    AND ETH = 'N' THEN 'African American'
               ELSE 'Other'
          END ) AS Ethnicity ,
        ( CASE WHEN s.U7 = 'G' THEN 'Yes'
               ELSE 'No'
          END ) AS Gate ,
        ( CASE WHEN s.U9 IN ( 'R', 'D', 'S' ) THEN 'Yes'
               ELSE 'No'
          END ) AS SpecialEd ,
        ( CASE WHEN s.LF = '1' THEN 'English Only'
               WHEN s.LF = '2' THEN 'Initially Fluent English Proficient'
               WHEN s.LF = '3' THEN 'English Learner'
               WHEN s.LF = '4' THEN 'Redesignated Fluent English Proficient'
               WHEN s.LF = '5' THEN 'TBD'
          END ) AS LF ,
          
        CASE WHEN  SUM(a.PID)  >= 2  
                  AND SUM(CASE WHEN a.DS = 'SUS'  THEN 1
                               ELSE 0
                          END) >= 1 THEN 'No'
             ELSE 'Yes'
        END AS [On Target?]
FROM    dbo.STU AS s 
        LEFT JOIN dbo.ADS AS a  ON a.PID = s.ID
WHERE  (a.DT >= '2013-09-01') AND
         s.GR = @GR
        AND s.SC = @SC
GROUP BY s.ID ,
        s.GR ,
        s.SC ,
        s.FN ,
        s.LN ,
        RC1 ,
        s.ETH ,
        s.U7 ,
        s.U9 ,
        s.LF
        


Cartesian Yak

RonnieRahman
Starting Member

United Kingdom
6 Posts

Posted - 02/25/2014 :  07:41:12  Show Profile  Reply with Quote
How about using a CTE first to filter your records and then LEFT JOIN to the CTE.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30114 Posts

Posted - 02/25/2014 :  11:29:20  Show Profile  Visit SwePeso's Homepage  Reply with Quote
As per email, move the date filter to the join.



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

Bustaz Kool
Flowing Fount of Yak Knowledge

USA
1643 Posts

Posted - 02/25/2014 :  19:18:48  Show Profile  Reply with Quote
By requiring a value for the non-preserved (other) table of the outer join in the where clause, you have converted it to an inner join.

===============================================================================
There are two kinds of light -- the glow that illuminates, and the glare that obscures. -James Thurber (1894-1961)
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.05 seconds. Powered By: Snitz Forums 2000