PostgreSQL ROUND函数

时间:2019-05-22 16:52:19

标签: sql postgresql

我尝试四舍五入此查询,但是我的结果有10个小数位。我试过DECIMAL()函数,CAST()ROUND(),但它仍然不起作用。 我需要2个小数位。

select type, count(id) filter ( where type ilike 
'private') as "number of applications",
   count(id) filter ( where type ilike 'private' and 
 type ilike 'paid') as "number of paid applications",
(count(id) filter ( where type ilike 'private' and 
status ilike 'paid')
     /round(count(id) filter (where type ilike 'private')) * 
(100)) as "percent of paid applications"
from applications
where type ilike 'private'
group by type
union
select type, count(id) filter ( where type ilike 
'business') as "number of applications",
      count(id) filter ( where type ilike 'private' and 
status ilike 'paid') as "number of paid applications",
      (count(id) filter ( where type ilike 'business' and 
status ilike 'paid')
       /round(count(id) filter (where type ilike 
'business')) * (100)) as "percent of paid applications"
from applications
where type ilike 'business'
group by type

2 个答案:

答案 0 :(得分:0)

round(yourvalue,2)应该确实想要

例如select round(12.1451124569,2)作为第一个参数的十进制精度为10的数字将返回12.15

答案 1 :(得分:0)

您要舍入结果。无需四舍五入分母计数。

...round(count(id) filter ( where type ilike 'business' and status ilike 'paid')
/(count(id) filter (where type ilike 'business')) * (100), 2) 
as "percent of paid applications"

或者如果这有点令人困惑,则可以将其作为s子查询并舍入结果:

select t.type, t.count, round(t."percent of paid applications", 2), t.etc from (your huge query here) as t;