我编写了以下查询来获取单个用户的计数和百分比值:
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。有人可以告诉我应该做什么改变才能根据我的要求获得%值。
最佳答案
您可以使用round function 。
select a.user_type,a.count,round(a.count * 100/(select sum(sessions) from user_type),2) ...
这应该将百分比值截断为小数点后两位。
关于sql - 如何在 PostgreSQL 中获得最多两位小数的百分比值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30046243/