更改SUM返回NULL为零

时间:2021-10-07 02:23:17

I have a Stored Procedure as follows:

我有一个存储过程如下:

CREATE PROC [dbo].[Incidents]
(@SiteName varchar(200))

AS

SELECT

(  
    SELECT SUM(i.Logged)  
    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

'tbl_Sites contains a list of reported on sites.
'tbl_Incidents containts a generated list of total incidents by site/date (monthly)
'If a site doesnt have any incidents that month it wont be listed.

The problem I'm having is that a site doesnt have any Incidents this month and as such i get a NULL value returned for that site when i run this sproc, but i need to have a zero/0 returned to be used within a chart in SSRS.

我遇到的问题是网站本月没有任何事件,因此当我运行此sproc时,我得到为该网站返回的NULL值,但我需要返回0/0才能在图表中使用在SSRS。

I've tried the using coalesce and isnull to no avail.

我尝试过使用coalesce并且无所作为。

    SELECT COALESCE(SUM(c.Logged,0))
    SELECT SUM(ISNULL(c.Logged,0))

Is there a way to get this formatted correctly?

有没有办法正确地格式化?

Cheers,

干杯,

Lee

背风处

6 个解决方案

#1


38  

Put it outside:

把它放在外面:

SELECT COALESCE(

(  
    SELECT SUM(i.Logged)  
    FROM tbl_Sites s  
    INNER JOIN tbl_Incidents i  
    ON s.Location = i.Location  
    WHERE s.Sites = @SiteName AND i.[month] = DATEADD(mm, DATEDIFF(mm, 0, GetDate()) -1,0)  
    GROUP BY s.Sites  
), 0)  AS LoggedIncidents

If you are returning multiple rows, change INNER JOIN to LEFT JOIN

如果要返回多行,请将INNER JOIN更改为LEFT JOIN

SELECT COALESCE(SUM(i.Logged),0)
FROM tbl_Sites s  
LEFT JOIN tbl_Incidents i  
ON s.Location = i.Location  
WHERE s.Sites = @SiteName AND i.[month] = DATEADD(mm, DATEDIFF(mm, 0, GetDate()) -1,0)  
GROUP BY s.Sites  

By the way, don't put any function or expression inside aggregate functions if it's not warranted, e.g. don't put ISNULL, COALESCE inside of SUM, using function/expression inside aggregation cripples performance, the query will be executed with table scan

顺便说一句,如果没有保证,不要在聚合函数中放置任何函数或表达式,例如不要把ISNULL,COALESCE放在SUM里面,使用function / expression里面的聚合瘫痪性能,查询将用表扫描执行

#2


19  

You'll have to use ISNULL like this -

你必须像这样使用ISNULL -

ISNULL(SUM(c.Logged), 0)      

Or, as Michael said, you can use a Left Outer Join.

或者,正如迈克尔所说,你可以使用左外连接。

#3


6  

I met this problem in oracle. Oracle does not have an ISNULL() function. However, we can use the NVL() function to achieve the same result:

我在oracle中遇到了这个问题。 Oracle没有ISNULL()函数。但是,我们可以使用NVL()函数来实现相同的结果:

NVL(SUM(c.Logged), 0)

#4


2  

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  

#5


1  

You could wrap the SELECT in another SELECT like so:

您可以将SELECT包装在另一个SELECT中,如下所示:

 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
)

#6


1  

Just ran into this problem, Kirtan's solution worked for me well, but the syntax was a little off. I did like this:

刚遇到这个问题,Kirtan的解决方案对我很有帮助,但语法有点偏差。我喜欢这样:

ISNULL(SUM(c.Logged), 0)

Post helped me solve my problem though so thanks to all.

邮件帮助我解决了我的问题,所以感谢所有。

#1


38  

Put it outside:

把它放在外面:

SELECT COALESCE(

(  
    SELECT SUM(i.Logged)  
    FROM tbl_Sites s  
    INNER JOIN tbl_Incidents i  
    ON s.Location = i.Location  
    WHERE s.Sites = @SiteName AND i.[month] = DATEADD(mm, DATEDIFF(mm, 0, GetDate()) -1,0)  
    GROUP BY s.Sites  
), 0)  AS LoggedIncidents

If you are returning multiple rows, change INNER JOIN to LEFT JOIN

如果要返回多行,请将INNER JOIN更改为LEFT JOIN

SELECT COALESCE(SUM(i.Logged),0)
FROM tbl_Sites s  
LEFT JOIN tbl_Incidents i  
ON s.Location = i.Location  
WHERE s.Sites = @SiteName AND i.[month] = DATEADD(mm, DATEDIFF(mm, 0, GetDate()) -1,0)  
GROUP BY s.Sites  

By the way, don't put any function or expression inside aggregate functions if it's not warranted, e.g. don't put ISNULL, COALESCE inside of SUM, using function/expression inside aggregation cripples performance, the query will be executed with table scan

顺便说一句,如果没有保证,不要在聚合函数中放置任何函数或表达式,例如不要把ISNULL,COALESCE放在SUM里面,使用function / expression里面的聚合瘫痪性能,查询将用表扫描执行

#2


19  

You'll have to use ISNULL like this -

你必须像这样使用ISNULL -

ISNULL(SUM(c.Logged), 0)      

Or, as Michael said, you can use a Left Outer Join.

或者,正如迈克尔所说,你可以使用左外连接。

#3


6  

I met this problem in oracle. Oracle does not have an ISNULL() function. However, we can use the NVL() function to achieve the same result:

我在oracle中遇到了这个问题。 Oracle没有ISNULL()函数。但是,我们可以使用NVL()函数来实现相同的结果:

NVL(SUM(c.Logged), 0)

#4


2  

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  

#5


1  

You could wrap the SELECT in another SELECT like so:

您可以将SELECT包装在另一个SELECT中,如下所示:

 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
)

#6


1  

Just ran into this problem, Kirtan's solution worked for me well, but the syntax was a little off. I did like this:

刚遇到这个问题,Kirtan的解决方案对我很有帮助,但语法有点偏差。我喜欢这样:

ISNULL(SUM(c.Logged), 0)

Post helped me solve my problem though so thanks to all.

邮件帮助我解决了我的问题,所以感谢所有。