有一个表格,其中有“数量”,“插入日期”和“结果”列。
Quantity || TIMESTAMP || RESULT
Sample 1 || 2019-01-28 18:21:04 || PASS
Sample 2 || 2019-01-30 18:21:04 || FAIL
以此类推
单个查询如何仅给出零件总数,通过的数量和失败的GROUPED BY日期的数量?
答案 0 :(得分:0)
如果您使用的是MySQL 5.x,则只能使用多个子查询。
select (select count(*) from Test ) as Total, passed.val, failed.val
from (select count(*) as val, tmstamp from Test where result = 'PASS' group by tmstamp) passed
, (select count(*) as val, tmstamp from Test where result = 'FAIL' group by tmstamp) failed
WHERE passed.tmstamp = failed.tmstamp;
如果您使用的是MySQL8,则应研究CTE(公用表表达式)功能。
WITH Passed as (select count(*) as val, tmstamp from Test where result = 'PASS' group by tmstamp),
Failed as (select count(*) as val, tmstamp from Test where result = 'FAIL' group by tmstamp)
select (select count(*) from Test ) as Total, p.val, f.val
From Passed p
join Failed f on f.tmstamp = p.tmstamp