我正在尝试进行一个简单的百分比计算,但无论我做什么,我都会为我的领域accuracy
0
。我也尝试将其选为DOUBLE
,但没有运气。两个输入都单独生成一个数字,所以我无法弄清楚为什么我总是得到 0。我只想在给定的月份做(# of TRUE
/Records
(。我怀疑这里的问题是我按月分组,但不知道如何解决。
我是否需要将计数作为除法的子查询,或者我可以在一个查询中完成所有这些查询吗?
桌子
+------------+-------+----+
| Date | reg | id |
+------------+-------+----+
| 2020-05-01 | TRUE | 1 |
+------------+-------+----+
| 2020-05-02 | TRUE | 2 |
+------------+-------+----+
| 2020-05-03 | FALSE | 3 |
+------------+-------+----+
| 2020-05-03 | TRUE | 4 |
+------------+-------+----+
| 2020-05-03 | TRUE | 5 |
+------------+-------+----+
| 2020-05-04 | FALSE | 6 |
+------------+-------+----+
查询
SELECT
SUBSTR(date, 1, 7) as month_year,
SUM(CASE WHEN reg = FALSE THEN 1 ELSE 0 END) as error,
SUM(CASE WHEN reg = TRUE THEN 1 ELSE 0 END) as valid,
COUNT(reg) as records,
ROUND(SUM(CASE WHEN reg = TRUE THEN 1 ELSE 0 END)/COUNT(reg), 2) as accuracy
FROM table
GROUP BY 1
ORDER BY 1 DESC
LIMIT 13
Presto大概会做整数除法。 但是,无论如何你都不需要分裂。 取代:
ROUND(SUM(CASE WHEN reg = TRUE THEN 1 ELSE 0 END)/COUNT(reg), 2) as accuracy
跟:
ROUND(AVG(CASE WHEN reg = TRUE THEN 1.0 ELSE 0 END), 2) as accuracy
= TRUE
是多余的,但我保留了它,因为您的代码中有它。