行之间的SQL比率

时间:2016-10-17 00:34:18

标签: sql sql-server apache-drill

我有一个具有以下格式的SQL表:

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

我想对此进行一个查询,它会按照function_id对结果进行分组,给出一个'失败的数字的比率。事件与'开始的数量'事件,以及维持失败的次数。即我想运行一个查询,它将提供如下所示的内容:

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

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

任何帮助将不胜感激! :)

1 个答案:

答案 0 :(得分:2)

这是所有条件聚合:

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