我想计算满足某些条件的行的分数。所以像这样:
(SELECT COUNT(*) FROM table WHERE col_name > crit) / (SELECT COUNT(*) FROM table)
但是,当我尝试时,这会产生语法错误。
我怎样才能实现我想要的?
正确的语法是:
select (select count(*) from table where col_name > crit)::decimal(10, 1) / count(*)
from table
但是您也可以(误)使用
avg
功能:
select avg(case when col_name > crit then 100.0 else 0.0 end)
from table