在关注我能找到的每个线程之后,我没有让它工作,我很想得到一些关于我可能做错了什么的意见。我正在使用 Coalesce 聚合多个 int2 字段。我只在每个字段都有值时才得到值;如果任何地方都有空值(这很常见),则整个聚合的结果为空。我知道 nullif() 函数可以用像 0 这样的可用值替换空值,但是无论我做什么,我要么得到一个错误,要么得到 0 作为整个聚合的结果。任何帮助表示赞赏!
这是代码:
SELECT
"public".jam_fodder_productivity.cows,
"public".jam_fodder_productivity.sheep,
"public".jam_fodder_productivity.lamb,
"public".jam_fodder_productivity.horse,
"public".jam_fodder_productivity.ungneyti,
COALESCE(jam_fodder_productivity.cows + (jam_fodder_productivity.ungneyti / 2::double precision) + (jam_fodder_productivity.horse / 2::double precision) + (jam_fodder_productivity.sheep / 6::double precision) + (jam_fodder_productivity.lamb / 12::double precision)) AS total_productivity
FROM
"public".jam_fodder_productivity
查询结果如下所示:
你必须申请
COALESCE(<expression>, 0)
到构成总和的每个表达式。
否则,如果任何表达式为 NULL,则整个总和变为 NULL。
所以条目将被计算为
COALESCE(jam_fodder_productivity.cows, 0)
+ COALESCE(jam_fodder_productivity.ungneyti / 2::double precision, 0)
+ COALESCE(jam_fodder_productivity.horse / 2::double precision, 0)
+ COALESCE(jam_fodder_productivity.sheep / 6::double precision, 0)
+ COALESCE(jam_fodder_productivity.lamb / 12::double precision, 0)
AS total_productivity
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句