COUNT() returns 分组的总行数 table

COUNT() returns the total number of rows in the grouped table

我有两个 table:

工作table:

[![在此处输入图片描述][1]][1]

FailedReason table 被 :

引用到作业 table

[![在此处输入图片描述][2]][2]

我的目标是根据失败原因计算失败率。

我的期望是第一列包含 failure reason name,第二列包含 total number of all jobs ('successful' + 'failed'),第三列列包含 total number of failed jobs 导致的原因,第四列包含 failure ratio 使用以下公式计算:失败计数(3 列)/ 总计数(2 列)* 100.

我的 sql 查询:

SELECT
  FailedReason.main_reason as "Failure reason",
  COUNT(job.name) AS "Total jobs",
  SUM(CASE WHEN job.status='failed' THEN 1 ELSE 0 END) AS "Total failed jobs",
  SUM(CASE WHEN job.status='failed' THEN 1 ELSE 0 END) / COUNT(job.name) * 100 AS "Failure ratio"
FROM job
LEFT JOIN FailedReason
ON job.id=FailedReason.job_id
GROUP BY 1
ORDER BY 3 DESC

我得到的结果是汇总 table 中的工作总数。结果失败率百分之一百

[![在此处输入图片描述][3]][3]

我应该修改什么以获得正确的作业数 ('succeeded' + 'failed') 并计算正确的比率值

示例数据:

CREATE TABLE failedreason (id INT PRIMARY KEY AUTO_INCREMENT,
                  job_id INT REFERENCES job(id),
                  main_reason varchar(255)
                 );



INSERT INTO failedreason (job_id, main_reason) VALUES (13095427, 'test case failure'),
                                    (13095407, 'test case failure'),
                                    (13095533, 'connection error'),
                                    (13095546, 'connection error'),
                                    (13098367, 'runner connection error'),
                                    (13101522, 'script error');

CREATE TABLE job (id INT PRIMARY KEY,
                  created_at date,
                  finished_at date,
                  status varchar(255)
                 );
INSERT INTO job (id,
                  created_at ,
                  finished_at ,
                  status
                 )
VALUES (13095427,  '2021-05-03 02:50:41', '2021-05-03 03:47:27', 'failed'),
       (13095407,  '2021-05-03 02:50:39', '2021-05-03 03:46:41', 'failed'),
       (13095533,  '2021-05-03 02:50:41', '2021-05-03 03:47:27', 'failed'),
       (13095546,  '2021-05-03 02:50:41', '2021-05-03 03:47:27', 'failed'),
       (13098367,  '2021-05-03 02:50:41', '2021-05-03 03:47:27', 'failed'),
       (13101522,  '2021-05-03 02:50:41', '2021-05-03 03:47:27', 'failed');
       (13101444,  '2021-05-03 02:50:41', '2021-05-03 03:47:27', 'success');
       (13101445,  '2021-05-03 02:50:41', '2021-05-03 03:47:27', 'success');
       (13101446,  '2021-05-03 02:50:41', '2021-05-03 03:47:27', 'success');



  [1]: https://i.stack.imgur.com/CYnPg.png
  [2]: https://i.stack.imgur.com/t3baS.png
  [3]: https://i.stack.imgur.com/LC7Hp.png

计算独特的工作,并汇总以获得总数。

SELECT
  CASE WHEN GROUPING(job.status) = 1 THEN 'TOTAL' ELSE job.status END AS `Job Status`
, CASE WHEN GROUPING(fail.main_reason) = 1 THEN 'TOTAL' ELSE fail.main_reason END AS `Failure Reason`
, COUNT(DISTINCT job.id) AS `Total Jobs`
, COUNT(DISTINCT job.id) / MAX(tots.total_jobs) * 100 AS `Ratio`
FROM job
CROSS JOIN (SELECT COUNT(*) AS total_jobs FROM job) tots
LEFT JOIN failedreason fail
ON job.id = fail.job_id
GROUP BY job.status, fail.main_reason WITH ROLLUP
ORDER BY GROUPING(job.status), GROUPING(fail.main_reason), `Ratio` DESC;
Job Status | Failure Reason          | Total Jobs |    Ratio
:--------- | :---------------------- | ---------: | -------:
success    | null                    |          3 |  33.3333
failed     | connection error        |          2 |  22.2222
failed     | test case failure       |          2 |  22.2222
failed     | runner connection error |          1 |  11.1111
failed     | script error            |          1 |  11.1111
failed     | TOTAL                   |          6 |  66.6667
success    | TOTAL                   |          3 |  33.3333
TOTAL      | TOTAL                   |          9 | 100.0000

演示 db<>fiddle here

使用SUM代替COUNT

SELECT f.main_reason AS "main reason",
       COUNT(*) AS "Total jobs",
       SUM(CASE WHEN j.status='failed' THEN 1 ELSE 0 END) AS "Total failed jobs",
       (SUM(CASE WHEN j.status='failed' THEN 1 ELSE 0 END) / COUNT(*)) * 100 AS "Failure ratio"
FROM job j
LEFT JOIN failedreason f ON j.id = f.job_id
GROUP BY f.main_reason

演示在 db<>fiddle

您的查询未提供正确的总数,因此 % 不正确。

请试试这个

select reason, alltotal, failed,
    failedtotal,
    cast( (100.00 * failed /failedtotal) as numeric(10,2)) failedRatio , 
    cast( (100.00 * failed /alltotal) as numeric(10,2)) failedPercent from 
    (SELECT
      FailedReason.[main_reason] reason,
      MAX(tots.total_jobs)  alltotal,
      MAX(ftots.total_failed)  failedtotal,
      COUNT(DISTINCT CASE WHEN job.status='failed' THEN job.id END) failed
    FROM job, FailedReason,
    (SELECT COUNT(*) AS total_failed FROM FailedReason) ftots , 
    (SELECT COUNT(*) AS total_jobs FROM job) tots
     where job.id = FailedReason.job_id 
    GROUP BY FailedReason.[main_reason]) finaltable

共享示例查询数据的结果