没有ISNULL()使用SUM()是否安全

时间:2021-10-19 04:09:47

I am trying to hence the performance of an SP. I have a doubt in my mind about SUM and ISNULL. When I sum up a column, should I use ISNULL? Is it SAFE to use SUM() without ISNULL. My example is below

我正试图通过SP的表现。我对SUM和ISNULL有一个疑问。当我总结一列时,我应该使用ISNULL吗?没有ISNULL使用SUM()是安全的吗?我的例子如下

SUM(ISNULL(COL1,0))

Since ISNULL costs a lot, I intend to use SUM without ISNULL like below

由于ISNULL成本很高,我打算在没有ISNULL的情况下使用SUM,如下所示

SUM(COL1)

I did some small tests and I couldnt see results

我做了一些小测试,但我看不到结果

4 个解决方案

#1


8  

Yes its safe . You can use Sum without handling NULL Value. You can also check that.

是的,它的安全。您可以使用Sum而不处理NULL值。你也可以检查一下。

You can use like that also.

你也可以这样使用。

ISNULL(SUM(COL1),0).

ISNULL(SUM(COL1),0)。

Returns the sum of all the values, or only the DISTINCT values, in the expression. SUM can be used with numeric columns only. Null values are ignored.

返回表达式中所有值或仅DISTINCT值的总和。 SUM只能与数字列一起使用。空值被忽略。

For Reference : https://msdn.microsoft.com/en-IN/library/ms187810.aspx

供参考:https://msdn.microsoft.com/en-IN/library/ms187810.aspx

#2


3  

Its not mandatory to use NULL and COALESCE while doing SUM because SUM always ignore null values.

在执行SUM时不必使用NULL和COALESCE,因为SUM始终忽略空值。

Refer the link : https://msdn.microsoft.com/en-IN/library/ms187810.aspx for more info.

有关详细信息,请参阅链接:https://msdn.microsoft.com/en-IN/library/ms187810.aspx。

#3


2  

Updated

更新

If you have [1, 2, NULL, 5] in 4 columns, it will give the output as 8.

如果4列中有[1,2,NULL,5],则输出为8。

However, it is not safe to use SUM() without checking for NULLS in many cases.

但是,在许多情况下使用SUM()而不检查NULLS是不安全的。

You can receive null when it has no matching content for a given clause. And if you are using this SUMMED value in another function, that maybe a point of concern.

如果给定子句没有匹配的内容,则可以接收null。如果您在另一个函数中使用此SUMMED值,那么这可能是一个值得关注的问题。

More details here: https://msdn.microsoft.com/en-GB/library/ms187810.aspx

更多细节:https://msdn.microsoft.com/en-GB/library/ms187810.aspx

Please also look at COALESCE method https://msdn.microsoft.com/en-IN/library/ms190349.aspx

另请参阅COALESCE方法https://msdn.microsoft.com/en-IN/library/ms190349.aspx

PS: Also check out this post - My Select SUM query returns null. It should return 0

PS:还看看这篇文章 - 我的选择SUM查询返回null。它应该返回0

Here are 3 images that shows without checking for NULL it returns NULL and not 0.

这里有3个图像显示没有检查NULL它返回NULL而不是0。

SUM with ISNULL CHECK 没有ISNULL()使用SUM()是否安全

与ISNULL CHECK的SUM

SUM without ISNULL CHECK

没有ISNULL CHECK的SUM

没有ISNULL()使用SUM()是否安全

SUM with COALESCE 没有ISNULL()使用SUM()是否安全

和COALESCE一起使用

#4


1  

Its better to use COALESCE method before the SUM aggregation.

最好在SUM聚合之前使用COALESCE方法。

#1


8  

Yes its safe . You can use Sum without handling NULL Value. You can also check that.

是的,它的安全。您可以使用Sum而不处理NULL值。你也可以检查一下。

You can use like that also.

你也可以这样使用。

ISNULL(SUM(COL1),0).

ISNULL(SUM(COL1),0)。

Returns the sum of all the values, or only the DISTINCT values, in the expression. SUM can be used with numeric columns only. Null values are ignored.

返回表达式中所有值或仅DISTINCT值的总和。 SUM只能与数字列一起使用。空值被忽略。

For Reference : https://msdn.microsoft.com/en-IN/library/ms187810.aspx

供参考:https://msdn.microsoft.com/en-IN/library/ms187810.aspx

#2


3  

Its not mandatory to use NULL and COALESCE while doing SUM because SUM always ignore null values.

在执行SUM时不必使用NULL和COALESCE,因为SUM始终忽略空值。

Refer the link : https://msdn.microsoft.com/en-IN/library/ms187810.aspx for more info.

有关详细信息,请参阅链接:https://msdn.microsoft.com/en-IN/library/ms187810.aspx。

#3


2  

Updated

更新

If you have [1, 2, NULL, 5] in 4 columns, it will give the output as 8.

如果4列中有[1,2,NULL,5],则输出为8。

However, it is not safe to use SUM() without checking for NULLS in many cases.

但是,在许多情况下使用SUM()而不检查NULLS是不安全的。

You can receive null when it has no matching content for a given clause. And if you are using this SUMMED value in another function, that maybe a point of concern.

如果给定子句没有匹配的内容,则可以接收null。如果您在另一个函数中使用此SUMMED值,那么这可能是一个值得关注的问题。

More details here: https://msdn.microsoft.com/en-GB/library/ms187810.aspx

更多细节:https://msdn.microsoft.com/en-GB/library/ms187810.aspx

Please also look at COALESCE method https://msdn.microsoft.com/en-IN/library/ms190349.aspx

另请参阅COALESCE方法https://msdn.microsoft.com/en-IN/library/ms190349.aspx

PS: Also check out this post - My Select SUM query returns null. It should return 0

PS:还看看这篇文章 - 我的选择SUM查询返回null。它应该返回0

Here are 3 images that shows without checking for NULL it returns NULL and not 0.

这里有3个图像显示没有检查NULL它返回NULL而不是0。

SUM with ISNULL CHECK 没有ISNULL()使用SUM()是否安全

与ISNULL CHECK的SUM

SUM without ISNULL CHECK

没有ISNULL CHECK的SUM

没有ISNULL()使用SUM()是否安全

SUM with COALESCE 没有ISNULL()使用SUM()是否安全

和COALESCE一起使用

#4


1  

Its better to use COALESCE method before the SUM aggregation.

最好在SUM聚合之前使用COALESCE方法。