SELECT min(a.id), a.value
FROM actions AS a
INNER JOIN (
SELECT a.call_id, a.id
FROM calls AS c
INNER JOIN actions AS a ON a.call_id = c.id
WHERE
c.created_at >= '2023-02-05 13:30:00'
AND c.created_at <= '2023-02-05 13:47:59'
AND `type` = 'MRCPSynth'
AND `value` -> "$.key" = 'select_free_dates'
) AS act ON a.call_id = act.call_id
WHERE
a.`type` = 'MRCPRecog'
AND a.id > act.id
GROUP BY a.value;
SELECT
a.value -> "$.text",
c.guid,
c.created_at
FROM (
SELECT ida, min(id) AS id
FROM (
SELECT MAX(a.id) AS ida, act.id
FROM (
SELECT a.id, a.call_id
FROM (
SELECT a.call_id, a.id
FROM calls AS c
INNER JOIN actions AS a ON a.call_id = c.id
WHERE
c.created_at >= '2023-02-05 13:30:00'
AND c.created_at <= '2023-02-05 13:47:59'
AND `type` = 'MRCPSynth'
AND `value` -> "$.key" = 'select_free_dates'
) AS act
INNER JOIN actions AS a ON a.call_id = act.call_id
WHERE
a.id > act.id
AND a.`type` = 'MRCPRecog'
) AS act
JOIN actions AS a ON a.call_id = act.call_id
WHERE
a.id < act.id
AND a.value -> "$.key" = 'select_free_dates'
GROUP BY a.id
) AS act
GROUP BY ida
) AS act
INNER JOIN actions AS a ON a.id = act.id
INNER JOIN calls AS c ON c.id = a.call_id;