如何使用 coalesce 求和

How to use coalesce to take sum

我可以在 coalesce() 中使用 sum() 吗?

我想在 Postgres 的存储函数中使用它。

例如

case (COALESCE(t3.Count3,0)+ COALESCE(t2.Count2,0) >= t4.Count4::float) 
then ( select (t4.Count4::float/((t3.Count3::float)+(t2.Count2::float))) * 100 as Count5 ) 
else ''0'' 
end as Count5

很难说出您在此处尝试做什么,因为代码中存在多个问题。此外,尽管您的标题提到 SUM,但您的代码不包含它(尽管您确实有 +,但那不一样)。

如果这是 SELECT 语句的一部分,那么我猜您想要的是:

SELECT
CASE
    WHEN COALESCE(t3.Count3,0)+ COALESCE(t2.Count2,0) >= t4.Count4::float
    THEN 100 * t4.Count4::float/(COALESCE(t3.Count3::float,0)+COALESCE(t2.Count2::float,0))
    ELSE 0
END as Count5
FROM MyTable