我有一个对我来说很麻烦的问题。我想从结果中去掉后面的零。
Remove trailing zeros from decimal in SQL Server
Remove trailing zeroes using sql

 select concat(100 * round(cast(count(ft.*) filter (where "Realtor_Sale" = 'Yes')
as numeric(12,5)) /
cast(count(ft.*) as numeric(12,5)),3),'%') as "Realtor Sales"

我得到的结果是:
84.800% --------------> I want 84.8%.

我也试过这么做:
select concat(100 * round(cast(cast(count(ft.*) filter (where "Realtor_Sale" = 'Yes')
as decimal(18,5)) as float) /
cast(cast(count(ft.*) as decimal(18,5)) as float),3), '%') as "Realtor Sales"

错误:
ERROR:  function round(double precision, integer) does not exist
select round(cast(cast(count(ft.*) filter (where "Realtor_Sa...

我怎样才能把结果精确到84.8%呢?

最佳答案

不需要很多类型转换,只需对结果使用to_char()

select to_char((100 * count(ft.*) filter (where "Realtor_Sale" = 'Yes'))::decimal
                / count(ft.*), 'FM99.0%') as "Realtor Sales"

关于sql - PostgreSQL删除尾随零,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42721281/

10-11 18:07