Count()返回0而不是NULL

时间:2012-03-15 17:55:50

标签: mysql sql tsql

我已经阅读了其他问题,答案似乎并没有解决我的情况。我有一张公司名称表和一份与这些公司相关的活动表。

我需要计算指定日期范围内的活动数量,如果没有,则返回0。我还需要获取最后一个活动的日期(即使它超出指定的日期范围)。

我正在使用LEFT OUTER JOIN,我已经尝试ISNULL()(但是,如果它不存在,它如何检查它是否为空?),我不知道从这里开始。

这就是我所拥有的:

SELECT       v_rpt_Company.Company_Name, COUNT(DISTINCT SO_Activity.SO_Activity_Recid) as touches, MAX(SO_Activity.Date_Entered) As lasttouch
FROM            v_rpt_Member LEFT OUTER JOIN
                         Company_Team ON v_rpt_Member.Member_RecID = Company_Team.Member_RecID LEFT OUTER JOIN
                         v_rpt_Company ON Company_Team.Company_RecID = v_rpt_Company.Company_RecID LEFT OUTER JOIN
                         SO_Activity ON v_rpt_Company.Company_RecID = SO_Activity.Company_RecID
WHERE        (Company_Team.AcctMgr_Flag = 1) AND (v_rpt_Member.Member_ID = @member) AND ((SO_Activity.Last_Update >= CONVERT(datetime, @date_start, 101)) AND ( SO_Activity.Last_Update <= CONVERT(datetime, @date_end, 101))) 
GROUP BY v_rpt_Company.Company_Name
ORDER BY v_rpt_Company.Company_Name ASC,lasttouch DESC,touches DESC

这是最终答案:(谢谢大家)

SELECT a.touches, a.name,b.lasttouch

FROM (

 SELECT       v_rpt_Company.Company_Name as name,
COUNT(DISTINCT SO_Activity.SO_Activity_Recid) as touches
FROM   v_rpt_Company  
LEFT OUTER JOIN  Company_Team  ON Company_Team.Company_RecID = v_rpt_Company.Company_RecID AND Company_Team.AcctMgr_Flag = 1
LEFT OUTER JOIN v_rpt_Member ON v_rpt_Member.Member_RecID = Company_Team.Member_RecID  
LEFT OUTER JOIN SO_Activity ON v_rpt_Company.Company_RecID = SO_Activity.Company_RecID AND ((SO_Activity.Last_Update >= CONVERT(datetime, @date_start, 101)) AND ( SO_Activity.Last_Update <= CONVERT(datetime, @date_end, 101))) 
WHERE v_rpt_Member.Member_ID = @member
Group By v_rpt_Company.Company_Name ) As a 

LEFT OUTER JOIN

(SELECT MAX(SO_Activity.Date_Entered) As lasttouch, v_rpt_Company.Company_Name as name

FROM   v_rpt_Company  
LEFT OUTER JOIN  Company_Team  ON Company_Team.Company_RecID = v_rpt_Company.Company_RecID AND Company_Team.AcctMgr_Flag = 1
LEFT OUTER JOIN v_rpt_Member ON v_rpt_Member.Member_RecID = Company_Team.Member_RecID
LEFT OUTER JOIN SO_Activity ON v_rpt_Company.Company_RecID = SO_Activity.Company_RecID

WHERE v_rpt_Member.Member_ID = @member 

GROUP BY v_rpt_Company.Company_Name) as b 

ON a.name = b.name

3 个答案:

答案 0 :(得分:3)

更新:尝试此操作,从v_rpt_company开始,因为您尝试按公司名称进行分组。

 SELECT       v_rpt_Company.Company_Name, 
COUNT(DISTINCT SO_Activity.SO_Activity_Recid) as touches, 
MAX(SO_Activity.Date_Entered) As lasttouch
FROM   v_rpt_Company  
LEFT OUTER JOIN  Company_Team  ON Company_Team.Company_RecID = v_rpt_Company.Company_RecID AND Company_Team.AcctMgr_Flag = 1
LEFT OUTER JOIN v_rpt_Member ON v_rpt_Member.Member_RecID = Company_Team.Member_RecID  
LEFT OUTER JOIN SO_Activity ON v_rpt_Company.Company_RecID = SO_Activity.Company_RecID AND ((SO_Activity.Last_Update >= CONVERT(datetime, @date_start, 101)) AND ( SO_Activity.Last_Update <= CONVERT(datetime, @date_end, 101))) 
WHERE v_rpt_Member.Member_ID = @member 
GROUP BY v_rpt_Company.Company_Name
ORDER BY v_rpt_Company.Company_Name ASC,lasttouch DESC,touches DESC

答案 1 :(得分:0)

像这样修改你的count语句:

COALESCE(COUNT(DISTINCT SO_Activity.SO_Activity_Recid),0)

答案 2 :(得分:0)

您的查询有两个问题:

  1. WHERE子句中的条件在该公司没有活动时将会为假(因为来自SO_Activity的值将为null,那么条件也将为null)。
  2. 如果上次活动的日期可能超出日期范围,则需要单独加入。
  3. 这是固定版本:

    SELECT 
    v_rpt_Company.Company_Name, 
    ISNULL(COUNT(DISTINCT SO_Activity.SO_Activity_Recid), 0) as touches, 
    MAX(SO2.Date_Entered) As lasttouch
    FROM v_rpt_Member 
    LEFT OUTER JOIN Company_Team ON v_rpt_Member.Member_RecID = Company_Team.Member_RecID
    LEFT OUTER JOIN v_rpt_Company ON Company_Team.Company_RecID = v_rpt_Company.Company_RecID 
    LEFT OUTER JOIN SO_Activity ON v_rpt_Company.Company_RecID = SO_Activity.Company_RecID
                          AND SO_Activity.Last_Update >= CONVERT(datetime, @date_start, 101) 
                          AND SO_Activity.Last_Update <= CONVERT(datetime, @date_end, 101)
    LEFT OUTER JOIN SO_Activity SO2 ON v_rpt_Company.Company_RecID = SO_Activity.Company_RecID
    WHERE Company_Team.AcctMgr_Flag = 1 AND v_rpt_Member.Member_ID = @member
    GROUP BY v_rpt_Company.Company_Name
    ORDER BY v_rpt_Company.Company_Name ASC,lasttouch DESC,touches DESC