PostgreSQL:计算列高于特定值的行的百分比

PostgreSQL: Calculate percentage of rows with a column above a certain value

假设我有 table 个值:

+----+-------+
| Id | Value |
+----+-------+
| 1  |  2    |
| 2  |  9    |
| 3  |  5    |
| 4  |  5    |
| 5  |  8    |
| 6  |  1    |
+----+-------+

如何计算值高于给定阈值的行的百分比:

例如,阈值为'higher than 5',我如何查询得到以下内容:

+-------+------------------+
| Total | Percent above 5  |
+-------+------------------+
|   6   |     33.33        |
+-------+------------------+

使用条件聚合:

select count(*),
       avg( (value > 5)::int ) * 100 as percentage_above_5
from t;