gpt4 book ai didi

sql - 聚合函数中的 Null

转载 作者:行者123 更新时间:2023-12-04 10:56:17 24 4
gpt4 key购买 nike

https://modern-sql.com/concept/null#aggregates说:

Think About It How does the propagation of null through expressions and the removal of null values prior to aggregation affect the following expressions:

SUM(a+b)

SUM(a) + SUM(b)



我不确定我是否理解它们的意思以及有什么区别。

我得到 NULL 上的聚合可能会给出 0。但是为什么这两个语句之间存在差异?

最佳答案

考虑这个数据:

a     b
1 NULL
NULL 2
3 3

表达式 sum(a + b)将要返回 6. 为什么?子项是:
1 + NULL  --> NULL
NULL + 2 --> NULL
3 + 3 --> 6
sum()其中的值为 6。

如果你做计算 sum(a) + sum(b) ,然后是 NULL值组合在每列中,您将得到 (1 + 3) + (2 + 3) --> 9.

Here是一个 db<>fiddle,说明了这种效果。

关于sql - 聚合函数中的 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59161550/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com