【发布时间】:2021-04-10 05:11:40
【问题描述】:
我正在使用 PostGres 10.12 DB,其中包含有关测试的各种字段:
|test_name|result |report_time|main_version|environment|
| A |error |29/11/2020 | 1 | john |
| A |failure|28/12/2020 | 1 | john |
| A |error |29/12/2020 | 1 | alice |
| B |passed |30/12/2020 | 2 | ben |
| C |failure|31/12/2020 | 2 | alice |
| A |error |31/12/2020 | 2 | john |
我正在尝试计算在同一天运行的所有测试中同时具有“失败/错误”和“通过”结果的测试百分比。
我创建了以下查询:
SELECT s.environment, COUNT(*) AS total, COUNT(*)::float / t.total_tests * 100 as percentage
FROM (
SELECT test_name, environment
FROM tests where report_time >= now() - interval '5 day'
and main_version='1' and environment='John'
GROUP BY test_name, environment
having COUNT(case when result in ('failure', 'error') then 1 else null end) > 0
and count(case when result = 'passed' then 1 else null end) > 0
order by environment asc
) s
CROSS JOIN (
SELECT COUNT(*) AS total_tests FROM tests where report_time >= now() - interval '5 day'
and main_version='1' and environment='John'
) t
GROUP BY s.environment, t.total_tests
这适用于 单一 环境和版本。当我尝试组合环境时,计数错误。
如何正确计算每天的正确百分比?
【问题讨论】:
-
样本数据和所需的结果会有所帮助。你是什么意思“在同一天运行”?您的示例代码中的条件是什么?问题没有提到他们。
标签: sql postgresql count percentage