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

时间:2015-05-05 07:03:21

标签: sql postgresql

我编写了以下查询来获取单个用户的计数和百分比值:

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

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

1 个答案:

答案 0 :(得分:1)

您可以使用round function

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

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