【问题标题】:Calculate percentage shows only 100 perc PostgreSQL计算百分比显示只有 100 perc PostgreSQL
【发布时间】:2020-06-25 17:06:31
【问题描述】:

我正在计算配给,但我不明白为什么它只显示 0 或 100。当我不把它放在一起时,这个公式可以正常工作:

case 
    when (count(status_id) filter (where status_id = 2)) = 0 then 0 
    else ((count(status_id) filter (where status_id = 2))/count(status_id))*100 end as conversion_rate

【问题讨论】:

    标签: sql postgresql group-by count percentage


    【解决方案1】:

    这称为整数除法:由于分子和分母都是整数值,Postgres 产生整数结果。

    为了避免它,你可以这样做:

    case when (count(status_id) filter (where status_id = 2)) = 0 
        then 0 else 
        100.0 * count(status_id) filter (where status_id = 2) / count(status_id) 
    end as conversion_rate
    

    【讨论】:

      猜你喜欢
      • 2017-05-13
      • 2013-10-27
      • 1970-01-01
      • 2020-10-20
      • 1970-01-01
      • 2021-12-24
      • 1970-01-01
      • 2023-02-11
      • 2011-06-01
      相关资源
      最近更新 更多