Home Forums SQL Server 2005 SQL Server Newbies LEFT JOIN not returning anticipated results from Left table in query RE: LEFT JOIN not returning anticipated results from Left table in query

  • Your WHERE clause is converting your LEFT JOIN into an INNER JOIN.

    You need to change your query a little bit.

    Here's a nice article on the subject: http://www.sqlservercentral.com/articles/T-SQL/93039/

    SELECT Users.Code AS FE

    ,COUNT(Matters.FeeEarnerRef) AS No_of_Matters

    ,ISNULL(SUM(Usr_Int1.Estimated_total_fee), 0) AS Fee_Estimate

    FROM Users

    LEFT JOIN Matters ON Users.Code = Matters.FeeEarnerRef

    AND created > '2014-03-16'

    INNER JOIN Usr_Int1 ON (Matters.Number = Usr_Int1.MatterNo)

    AND (Matters.EntityRef = Usr_Int1.EntityRef)

    WHERE (users.feeearner = 1)

    AND usertyperef <> 7

    GROUP BY code

    ,usertyperef

    ORDER BY code

    And you don't need the HAVING clause, it could easily go into the WHERE clause.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2