整数除法返回0

时间:2014-10-23 20:45:45

标签: sql postgresql division

我觉得我错过了一些明显的东西。我试图测试random()的分布。这是表格:

create table test (
  id int,
  random_float float,
  random_int int
);

这是我想要做的:

truncate table test;
insert into test (id)
values (generate_series(1,1000));
update test 
set
  random_float = random() * 10 + 1;
update test
set
  random_int = trunc(random_float);
select 
  random_int,
  count(random_int) as Count,
  cast( count(random_int) / max(id) as float) as Percent
from test
group by random_int
order by random_int;

但是,“Percent”列为每条记录返回零。我尝试将其转换为float,作为十进制,我尝试将random_int列更改为十进制而不是整数,总是相同的结果。

Here is a fiddle.

有关我做错的任何见解?

2 个答案:

答案 0 :(得分:10)

你应该在划分之前进行投射,但是你也缺少一个子查询来从表中获得总数。 Here's the sample

select 
  random_int,
  count(random_int) as Count,
  cast(count(random_int) as decimal(7,2)) / cast((select count(random_int) from test) as decimal(7,2)) as Percent
from test
group by random_int
order by random_int;

答案 1 :(得分:2)

请尝试此查询:

select 
       random_int,
       count(random_int) as Count,
       cast( count(random_int) / max(id) as float) as Percent,
       (100.0 * count(random_int) / max(id))::numeric(5,2) as pct
  from test
 group by random_int
 order by random_int;

PostgreSQL有一个强大的类型系统。在您的情况下,count()函数隐含了类型,该函数返回bigint(或int8)和id列,即integer

我建议使用100.0作为初始乘数,这将导致整个表达式计算为数字,并且还将提供真实百分比。您可能还希望在结尾处转换为numeric(5,2)以消除太大的数字。

相关问题