gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:27:59 28 4
gpt4 key购买 nike

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

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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com