Changing a SUM returned NULL to zero

前端 未结 6 1751
旧巷少年郎
旧巷少年郎 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:33

    You could wrap the SELECT in another SELECT like so:

     CREATE PROC [dbo].[Incidents]
    (@SiteName varchar(200))
    
    AS
    
    SELECT COALESCE(TotalIncidents  ,0)
    FROM (
      SELECT
      (  
        SELECT SUM(i.Logged) as TotalIncidents  
        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  
      )  AS LoggedIncidents
    )
    

提交回复
热议问题