【发布时间】:2016-04-17 20:49:13
【问题描述】:
我的 SELECT 语句中的两个参数是:
count(product_code.code) as codes_allocated
count(case when product_code.taken='TRUE' then product_code.taken end) as codes_claimed
我想在我的 select 语句中再添加一个参数,该参数采用 codes_claimed 并将其除以 codes_allocated 以获得已声明代码的百分比。
我尝试了很多东西,但总是得到错误:
错误:除以零
查询失败。
我最近的尝试使用了以下内容:
(count(case when core_isvcode.reserved='TRUE' then core_isvcode.reserved end)::decimal / count(core_isvcode.code)::decimal)*100 as Percent_redeemed`
非常感谢任何帮助和指导!
【问题讨论】:
-
在什么情况下
codes_allocated可以为0?也许您应该过滤掉codes_allocated为0 的行。 -
添加新产品或将代码传输到另一个系统时,计数可能会降至零。一个好主意,但我还不确定如何解决。会做一些挖掘。
标签: sql database postgresql percentage