select count() from table where col=yes 的结果除以 select count() from table 的结果
请教下这个语句该怎么写呢?
select (select count() from table where col=yes)/ (select count() from table);
楼上这个答案有点敷衍。。。
简单来说,题主是想实现,在一个表里面,满足某个条件的记录数占总记录数的百分比,其实count里就可以直接用case when 来过滤数据
select count(case when col=yes then 1 end )/count(1) from table ;
--或
select sum(case when col=yes then 1 eles 0 end )/count(1) from table ;
很奇怪,除的结果总是0,单算分子正常整数,单算分母正常整数,可是一除结果就是0,数据库是db2,难道是因为数据库的原因?