SQL Count On日期

时间:2013-12-18 13:05:29

标签: sql reporting-services count database-table

我正在处理使用SSRS 2008的报告

我有代码可以带回2列DateAppRcvd和Advisor

以下是我的代码:

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

这会把我带回来

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

如何抛弃格雷姆。我知道为什么这样做,因为这个用户没有日期/计数。

有没有办法将它设置为在DateAppRcvd

中将Graeme带回0

2 个答案:

答案 0 :(得分:0)

假设在Graeme的情况下,DateAppRcvd列为NULL,这应该会有所帮助:


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

答案 1 :(得分:0)

如果Graeme在tbl_Profile_Mortgage中没有条目,那么您需要移动连接并使用外连接(在morgage配置文件和employees表之间)来显示空值。一旦出现空值,这将导致记录在聚合函数中计算。

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