我正在计算5个最后一个进程执行的平均值,因为我使用子查询,因为limit子句将计算所有执行的平均值而不仅仅是5.我的问题是在我的子查询中我有一个未知的列错误where子句。 我应该使用join而不是子查询吗?我怎样才能做到这一点?
SELECT s.id, s.name,ssch.name,s.refresh_delay,
(SELECT MAX(start_time) FROM runs WHERE sp_id = s.id) AS last_run_started,
(SELECT MAX(start_time) FROM runs WHERE sp_id = s.id AND sp_run_status_id = 27 AND confidence_level >= 90 AND manual_approval_id IN (0,1)) AS last_run_accepted,
(SELECT last_run_started + INTERVAL 15 MINUTE) AS dwt,
(SELECT SEC_TO_TIME(AVG(TIME_TO_SEC(times.completion_time) - TIME_TO_SEC(times.start_time))) AS tw FROM
(SELECT completion_time, start_time FROM runs **WHERE sp_id = s.id** LIMIT 5) times) tw
FROM spouses s
LEFT JOIN `time_zones` tz ON tz.`id` = `time_zone_id`
答案 0 :(得分:1)
这是您的查询,简化为仅显示问题的重要部分:
SELECT s.id, s.name, ssch.name, s.refresh_delay,
(SELECT SEC_TO_TIME(AVG(TIME_TO_SEC(times.completion_time) -TIME_TO_SEC(times.start_time))) AS tw
FROM (SELECT completion_time, start_time
FROM runs
**WHERE sp_id = s.id**
LIMIT 5
) times
) tw
FROM spouses s LEFT JOIN
`time_zones` tz
ON tz.`id` = `time_zone_id`;
这个逻辑有两个问题:
limit
而不使用order by
是可疑的。一种方法是在FROM
子句中将聚合作为子查询进行,使用变量枚举每个sp_id
的行:
SELECT s.id, s.name, ssch.name, s.refresh_delay,
t.tw
FROM spouses s LEFT JOIN
`time_zones` tz
ON tz.`id` = `time_zone_id` LEFT JOIN
(SELECT sp_id, SEC_TO_TIME(AVG(TIME_TO_SEC(times.completion_time) -TIME_TO_SEC(times.start_time))) AS tw
FROM (SELECT sp_id, completion_time, start_time,
(@rn := if(@spid = sp_id, @rn + 1,
if(@spid := sp_id, 1, 1)
)
) as seqnum
FROM runs r CROSS JOIN
(SELECT @spid := 0, @rn := 0) params
ORDER BY sp_id, start_time desc
) times
WHERE seqnum <= 5
GROUP BY sp_id
) t
ON t.sp_id = s.id