sql - 如何在 PostgreSQL 中获得最多两位小数的百分比值?

标签 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。有人可以告诉我应该做什么改变才能根据我的要求获得%值。

最佳答案

您可以使用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/

相关文章:

mysql - 数据库规范化用户和消息数据库

sql - 动态查询中的条件 WHERE 表达式

ruby-on-rails - 将 Postgres Schema 与 Ruby on Rails 结合使用

postgresql - 使用比较运算符比较 postgres 中的字符串?

sql - 如何对每一行 PostgreSQL 使用计数

c# - 从 C# 中的 PL/SQL 匿名 block 返回游标

postgresql - 在 pgadmin 4 上删除服务器

sql - 从 JSONB 列中按键选择不同的值

sql - 从一个表中选择值,其中排除另一个表中的值

mysql - 将记录列表与数据库中的表进行比较并列出它们的时间戳