兩個參數:如何找到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