两个参数:如何找到postgresql中两列之间的百分比?在我的SELECT语句
count(product_code.code) as codes_allocated
count(case when product_code.taken='TRUE' then product_code.taken end) as codes_claimed
我想多一个参数添加到我的选择语句,采用codes_claimed并通过codes_allocated除以它获得的代码的百分比要求。
我尝试过很多事情,但总是出现错误:
ERROR: division by zero
Query failed.
我最近尝试使用以下:
(count(case when core_isvcode.reserved='TRUE' then core_isvcode.reserved end)::decimal/count(core_isvcode.code)::decimal)*100 as Percent_redeemed`
任何帮助和指导,不胜感激!
'code_allocated'在什么情况下可以为0?也许你应该过滤掉'codes_allocated'为0的行。 – absolutelyNoWarranty
当添加新产品时,或者代码被传输到另一个系统时,计数可以降到零。一个好主意,但我不确定如何解决。会做一些挖掘。 – Jacktheyeti