如何在PostgreSQL中获得高达两位小数的百分比值?

时间:2020-12-30 17:06:38

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.

百分比值如7.35684648。有人可以告诉我,根据我的要求,我应该做些什么改变来获得%值。

1 个解决方案

#1


1  

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.

这应该将百分比值截断为2位小数。

#1


1  

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.

这应该将百分比值截断为2位小数。