I would like to calculate the fraction of rows fulfilling some criteria. So something like this:
(SELECT COUNT(*) FROM table WHERE col_name > crit) / (SELECT COUNT(*) FROM table)
However, this creates a syntax error when I tried it.
How can I realize what I want?
>Solution :
The correct syntax would be:
select (select count(*)::decimal from table where col_name > crit) / count(*)::decimal
from table
But you can (mis)use the avg function too:
select avg(case when col_name > crit then 1.0 else 0.0 end)
from table