Division of integers returns 0

后端 未结 2 374
天命终不由人
天命终不由人 2020-11-30 12:23

I feel like I\'m missing something obvious. I am trying to test out the distribution of random(). Here is the table:

create table test (
  id in         


        
2条回答
  •  鱼传尺愫
    2020-11-30 13:06

    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;
    

提交回复
热议问题