Question

Im working on reports using SSRS 2008

I Have code that brings back 2 columns DateAppRcvd and Advisor

Here is my code below:

SELECT
      COUNT(pm.DateAppRcvd) AS [DateAppRcvd],
     u.FullName AS [Advisor]
FROM  tbl_Profile_Mortgage AS pm
       INNER JOIN tbl_Profile AS p
            ON pm.FK_ProfileId =  p.Id 
       INNER JOIN tbl_User u
            ON p.Advisor = u.UserId
WHERE DateAppRcvd Between '01-Nov-2013 00:00:00.000' AND '30-Nov-2013 23:59:00.000'
AND pm.AccelInd <>'1' 
and  u.FullName in ('Colin Sneddon ','Graeme Hastie','Jonathon Bede Pratt','Mark Reidy','Neil Jones','Nigel Crook','Sharon Parouty','Tom McSherry')
GROUP BY u.FullName

This will Bring me back

DateAppRcvd Advisor
11          Colin Sneddon
1           Jonathon Bede Pratt
17          Mark Reidy
4           Neil Jones
5           Nigel Crook
14          Sharon Parouty
8           Tom McSherry

How ever leaving out Graeme . I know why its doing this as there is no date / count for this user.

Is there a way to set it to bring back Graeme with a count of 0 in the DateAppRcvd

Was it helpful?

Solution

Assuming that in Graeme's case the column DateAppRcvd is NULL, this should help:


SELECT 
    COUNT(pm.DateAppRcvd) AS [DateAppRcvd], 
    u.FullName AS [Advisor] 
FROM 
    tbl_Profile_Mortgage AS pm 
    INNER JOIN tbl_Profile AS p ON pm.FK_ProfileId = p.Id 
    INNER JOIN tbl_User u ON p.Advisor = u.UserId 
WHERE 
    (
        DateAppRcvd Between '01-Nov-2013 00:00:00.000' AND '30-Nov-2013 23:59:00.000' 
        OR
        DateAppRcvd IS NULL
    )
    AND pm.AccelInd '1' 
    and u.FullName in ('Colin Sneddon ','Graeme Hastie','Jonathon Bede Pratt','Mark Reidy','Neil Jones','Nigel Crook','Sharon Parouty','Tom McSherry')
GROUP BY u.FullName

OTHER TIPS

If Graeme doesn't have an entry in tbl_Profile_Mortgage, then you need to move the joins around and use an outer join (between morgage profile and employees table) to make nulls appear. Once the nulls appear this will cause the records to be counted in the aggregation function.

SELECT
      COUNT(pm.DateAppRcvd) AS [DateAppRcvd],
     u.FullName AS [Advisor]
FROM tbl_User u
       INNER JOIN tbl_Profile AS p
            ON p.Advisor = u.UserId
       LEFT OUTER JOIN tbl_Profile_Mortgage AS pm
            ON pm.FK_ProfileId =  p.Id 
WHERE DateAppRcvd Between '01-Nov-2013 00:00:00.000' AND '30-Nov-2013 23:59:00.000'
AND pm.AccelInd <>'1' 
and  u.FullName in ('Colin Sneddon ','Graeme Hastie','Jonathon Bede Pratt','Mark Reidy','Neil Jones','Nigel Crook','Sharon Parouty','Tom McSherry')
GROUP BY u.FullName
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top