Annie
Annie

Reputation: 33

How to do percentage in Postgresql

I'm trying to create a query. I have two tables

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;

But the result is zero.

Upvotes: 2

Views: 5325

Answers (1)

Christian
Christian

Reputation: 7310

When all "numbers" involved in an operation are integers, the result will be an integer too.

Try this way:

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;

Upvotes: 4

Related Questions