mysql 多个分组在一个 table
mysql multiple grouping on one table
我有一个tablejob_result
CREATE TABLE job_result (
node varchar(20) DEFAULT NULL,
jobId int(10) DEFAULT NULL,
subResult int(1) DEFAULT NULL
)
table 包含很多节点。
每个节点有多个jobId。
每个jobId有多个subResults(通常少于10个)
提供了一些示例数据
insert into job_result values ('A', 14, 0);
insert into job_result values ('A', 15, 0);
insert into job_result values ('A', 16, 1);
insert into job_result values ('A', 17, 0);
insert into job_result values ('A', 18, 1);
insert into job_result values ('A', 19, 1);
insert into job_result values ('A', 20, 0);
insert into job_result values ('B', 1, 0);
insert into job_result values ('B', 2, 0);
insert into job_result values ('B', 3, 1);
insert into job_result values ('B', 4, 1);
insert into job_result values ('B', 5, 1);
insert into job_result values ('B', 6, 1);
insert into job_result values ('B', 7, 1);
insert into job_result values ('C', 10, 0);
insert into job_result values ('C', 11, 0);
insert into job_result values ('C', 12, 0);
insert into job_result values ('C', 13, 0);
insert into job_result values ('C', 14, 0);
insert into job_result values ('C', 15, 0);
insert into job_result values ('C', 16, 0);
我想要一个查询在每个节点上给我一个 "health check" 以查看某个节点是否有 X 个连续失败的作业。失败的作业是至少有一个非零结果的 jobId。
SELECT node, jobId, SUM(subResult) res
FROM job_result WHERE node = 'A'
GROUP BY jobId
ORDER BY jobId desc
LIMIT 5
这为我提供了节点 A 上最后 5 个作业的结果,其中 0 表示作业很好。
+------+---------+------+
| node | jobId | res |
+------+---------+------+
| A | 20 | 0 |
| A | 19 | 1 |
| A | 18 | 1 |
| A | 17 | 0 |
| A | 16 | 1 |
然后我需要对每个节点的结果求和并分组到一行中,我做到了这一点:
SELECT node, SUM(I.res) res
FROM
(SELECT node, sum(subResult) res
FROM job_result WHERE node = 'A'
GROUP BY jobId
ORDER BY jobId desc
LIMIT 5) I
+------+------+
| node | res |
+------+------+
| A | 3 |
我的问题是:如何将此查询扩展为 return 每个节点一行?
我已经尝试了几天相关的子查询和连接,但我总是失败,因为内部 select 看不到要 select 的哪个节点。
到位后,我将努力将 LIMIT 5 更改为动态值。
想要的结果就像
+------+------+
| node | res |
+------+------+
| A | 3 |
| B | 0 |
| C | 1 |
| E | 0 |
每个节点一行,右列显示失败次数
请指教!
在 MySQL 中,在大型 table 中,获得最后 5 个结果的最佳方法是使用变量来枚举结果。剩下的只是聚合:
SELECT node, SUM(subr > 0) as numfails
FROM (SELECT node, jobid, subr,
(@rn := if(@n = node, @rn + 1,
if(@n := node, 1, 1)
)
) as rn
FROM (SELECT node, jobid,
SUM(subresult) as subr
FROM job_result jr
GROUP BY node, jobid
) jr CROSS JOIN
(SELECT @n := '', @rn := 0) init
ORDER BY node, jobid desc
) nr
WHERE rn <= 5
GROUP BY node;
我有一个tablejob_result
CREATE TABLE job_result (
node varchar(20) DEFAULT NULL,
jobId int(10) DEFAULT NULL,
subResult int(1) DEFAULT NULL
)
table 包含很多节点。
每个节点有多个jobId。
每个jobId有多个subResults(通常少于10个)
提供了一些示例数据
insert into job_result values ('A', 14, 0);
insert into job_result values ('A', 15, 0);
insert into job_result values ('A', 16, 1);
insert into job_result values ('A', 17, 0);
insert into job_result values ('A', 18, 1);
insert into job_result values ('A', 19, 1);
insert into job_result values ('A', 20, 0);
insert into job_result values ('B', 1, 0);
insert into job_result values ('B', 2, 0);
insert into job_result values ('B', 3, 1);
insert into job_result values ('B', 4, 1);
insert into job_result values ('B', 5, 1);
insert into job_result values ('B', 6, 1);
insert into job_result values ('B', 7, 1);
insert into job_result values ('C', 10, 0);
insert into job_result values ('C', 11, 0);
insert into job_result values ('C', 12, 0);
insert into job_result values ('C', 13, 0);
insert into job_result values ('C', 14, 0);
insert into job_result values ('C', 15, 0);
insert into job_result values ('C', 16, 0);
我想要一个查询在每个节点上给我一个 "health check" 以查看某个节点是否有 X 个连续失败的作业。失败的作业是至少有一个非零结果的 jobId。
SELECT node, jobId, SUM(subResult) res
FROM job_result WHERE node = 'A'
GROUP BY jobId
ORDER BY jobId desc
LIMIT 5
这为我提供了节点 A 上最后 5 个作业的结果,其中 0 表示作业很好。
+------+---------+------+
| node | jobId | res |
+------+---------+------+
| A | 20 | 0 |
| A | 19 | 1 |
| A | 18 | 1 |
| A | 17 | 0 |
| A | 16 | 1 |
然后我需要对每个节点的结果求和并分组到一行中,我做到了这一点:
SELECT node, SUM(I.res) res
FROM
(SELECT node, sum(subResult) res
FROM job_result WHERE node = 'A'
GROUP BY jobId
ORDER BY jobId desc
LIMIT 5) I
+------+------+
| node | res |
+------+------+
| A | 3 |
我的问题是:如何将此查询扩展为 return 每个节点一行? 我已经尝试了几天相关的子查询和连接,但我总是失败,因为内部 select 看不到要 select 的哪个节点。 到位后,我将努力将 LIMIT 5 更改为动态值。
想要的结果就像
+------+------+
| node | res |
+------+------+
| A | 3 |
| B | 0 |
| C | 1 |
| E | 0 |
每个节点一行,右列显示失败次数
请指教!
在 MySQL 中,在大型 table 中,获得最后 5 个结果的最佳方法是使用变量来枚举结果。剩下的只是聚合:
SELECT node, SUM(subr > 0) as numfails
FROM (SELECT node, jobid, subr,
(@rn := if(@n = node, @rn + 1,
if(@n := node, 1, 1)
)
) as rn
FROM (SELECT node, jobid,
SUM(subresult) as subr
FROM job_result jr
GROUP BY node, jobid
) jr CROSS JOIN
(SELECT @n := '', @rn := 0) init
ORDER BY node, jobid desc
) nr
WHERE rn <= 5
GROUP BY node;