SQL 行间比率

SQL ratio between rows

我有一个 SQL table 格式如下:

+------------------------------------+
| function_id | event_type | counter |
+-------------+------------+---------+
| 1           | fail       | 1000    |
| 1           | started    | 5000    |
| 2           | fail       | 800     |
| 2           | started    | 4500    |
| ...         | ...        | ...     |
+-------------+------------+---------+

我想 运行 对此进行查询,该查询将按 function_id 对结果进行分组,方法是给出 'fail' 事件数与 [=22] 事件数的比率=] 事件,以及维护失败的次数。 IE。我想要 运行 一个查询,它会给出如下所示的内容:

+-------------------------------------+
| function_id | fail_ratio | failures |
+-------------+------------+----------+
| 1           | 20%        | 1000     |
| 2           | 17.78%     | 800      |
| ...         | ...        |          |
+-------------+------------+----------+

我尝试了一些方法,但到目前为止都没有成功。我目前正在使用 Apache Drill SQL,因为这些数据是从平面文件中提取的。

如有任何帮助,我们将不胜感激! :)

这都是条件聚合:

select function_id,
       sum(case when event_type = 'fail' then counter*1.0 end) / sum(case when event_type = 'started' then counter end) as fail_start_ratio,
       sum(case when event_type = 'fail' then counter end) as failures
from t
group by function_id