Madhusudan
Madhusudan

Reputation: 4815

How to get percentage value upto two decimal points in PostgreSQL?

I have written following query to get the count and percentage value for individual user:

select a.user_type,a.count,a.count * 100/(select sum(sessions) from user_type) as percentage
from
(select user_type, sum(sessions) as count 
from user_type 
group by user_type
order by user_type)  a,
(select user_type, sum(sessions) as count 
from user_type 
group by user_type
order by user_type)  b
where a.user_type = b.user_type and a.count = b.count
group by a.user_type,a.count

The percentage values are coming like 7.35684648. Can someone tell me what changes shall I do to get the % value as per my requirement.

Upvotes: 2

Views: 7075

Answers (1)

CodeNewbie
CodeNewbie

Reputation: 2091

You can use the round function.

select a.user_type,a.count,round(a.count * 100/(select sum(sessions) from user_type),2) ...

This should truncate the percentage value to 2 decimal places.

Upvotes: 3

Related Questions