Division of integers returns 0

You should cast before you divide, but also you were missing a subquery to get the total count from the table. 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;

Leave a Comment