У меня есть запрос на mariaDB:
SELECT AVG(selectOnStop.time_avg) AS global_avg_time,
AVG(selectFromStopTaskProcessor.time_avg) AS qmr_task_time,
AVG(selectOnStop.time_avg) - AVG(selectFromStopTaskProcessor.time_avg) AS postprocessing
FROM (SELECT
global_task_id,
SUM(time_from_start),
SUM(time_from_start) / 1000000 AS time_avg
FROM task AS t
INNER JOIN recorderevent AS r ON (CONCAT(r.task_id, '0', SUBSTRING(r.`shard_alias` FROM 7)) = t.id)
WHERE r.event_name = 'STOP'
GROUP BY global_task_id
ORDER BY SUM(time_from_start) DESC) AS selectOnStop
JOIN (SELECT global_task_id, SUM(time_from_start) / 1000000 AS time_avg
FROM task AS t
INNER JOIN recorderevent AS r ON (CONCAT(r.task_id, '0', SUBSTRING(r.`shard_alias` FROM 7)) = t.id)
WHERE r.event_name = 'QMR_STOP_TASK_PROCESSOR'
GROUP BY global_task_id
ORDER BY SUM(time_from_start) DESC) AS selectFromStopTaskProcessor;
Запрос работает. Я решил переписать его с использованием оператора WITH.
WITH selectOnStop (time_start, time_avg) AS (SELECT
SUM(time_from_start) AS time_start,
SUM(time_from_start) / 1000000 AS time_avg
FROM task AS t
INNER JOIN recorderevent AS r
ON (CONCAT(r.task_id, '0', SUBSTRING(r.`shard_alias` FROM 7)) = t.id)
WHERE r.event_name = 'STOP'
GROUP BY global_task_id
ORDER BY SUM(time_from_start) DESC)
,
selectFromStopTaskProcessor (time_average) AS (SELECT SUM(time_from_start) / 1000000 AS time_average
FROM task AS t
INNER JOIN recorderevent AS r
ON (CONCAT(r.task_id, '0', SUBSTRING(r.`shard_alias` FROM 7)) = t.id)
WHERE r.event_name = 'QMR_STOP_TASK_PROCESSOR'
GROUP BY global_task_id
ORDER BY SUM(time_from_start) DESC)
SELECT AVG(selectOnStop.time_avg) AS global_avg_time,
AVG(selectFromStopTaskProcessor.time_average) AS qmr_task_time,
AVG(selectOnStop.time_avg) - AVG(selectFromStopTaskProcessor.time_average) AS postprocessing
FROM selectOnStop
JOIN selectFromStopTaskProcessor;
По сути, я просто перенёс селекты в отдельные запросы. Пишет следующее:
[42000][1064] (conn=3) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'selectOnStop (time_start, time_avg) AS (SELECT [42000][1064] You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'selectOnStop (time_start, time_avg) AS (SELECT ' at line 1 Query is: WITH selectOnStop (time_start, time_avg) AS (SELECT SUM(time_from_start) AS time_sta ...
Подскажите, что я делаю не так, и что надо поправить в запросе, чтобы он работал?