【问题标题】:How to do percentage in Postgresql如何在 Postgresql 中做百分比
【发布时间】:2014-11-25 02:05:05
【问题描述】:

我正在尝试创建一个查询。我有两张桌子

  • customer (customer_id)
  • rental (rental_date (时间戳), customer_id)
SELECT   to_char( rental_date, 'Month') AS month, 
         (count(Distinct customer.customer_id)/
           (count (distinct rental.customer_id) * 100) 
         ) AS percentage
FROM RENTAL,customer
GROUP BY month;

但结果为零。

【问题讨论】:

    标签: postgresql percentage


    【解决方案1】:

    当运算中涉及的所有“数字”都是整数时,结果也将是整数。

    试试这个方法:

    SELECT 
      to_char( rental_date, 'Month') AS month, 
    
      (
        count(distinct customer.customer_id)::float / 
       (count(distinct   rental.customer_id)::float * 100::float) 
      ) 
       as percentage 
    
    FROM 
      RENTAL,customer 
    GROUP BY month;
    

    【讨论】:

      猜你喜欢
      • 2017-02-21
      • 1970-01-01
      • 2020-10-20
      • 2017-05-13
      • 1970-01-01
      • 2015-03-06
      • 1970-01-01
      • 1970-01-01
      • 2016-04-17
      相关资源
      最近更新 更多