简体   繁体   中英

How to use coalesce to take sum

Can I use sum() in coalesce() ?

I want to use it in a stored function in Postgres.

For example

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

It's hard to tell what you're trying to do here because there are multiple issues in the code. Also though your title mentions SUM your code doesn't include it (though you do have + but that's not the same).

If this is part of a SELECT statement, then I'm guessing what you want is:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM