Changing a SUM returned NULL to zero

前端 未结 6 1727
旧巷少年郎
旧巷少年郎 2021-02-05 01:54

I have a stored procedure as follows:

CREATE PROC [dbo].[Incidents]
(@SiteName varchar(200))
AS
SELECT
(  
    SELECT SUM(i.Logged)  
    FROM tbl_Sites s  
             


        
6条回答
  •  囚心锁ツ
    2021-02-05 02:25

    The easiest, and most readable, way I've found to accomplish this is through:

    CREATE PROC [dbo].[Incidents]
    (@SiteName varchar(200))
    
    AS
    
        SELECT SUM(COALESCE(i.Logged, 0)) AS LoggedIncidents
        FROM tbl_Sites s  
        INNER JOIN tbl_Incidents i  
        ON s.Location = i.Location  
        WHERE s.Sites = @SiteName 
              AND i.[month] = DATEADD(mm, DATEDIFF(mm, 0, GetDate()) -1,0)  
        GROUP BY s.Sites  
    

提交回复
热议问题