I need to convert this code to SQLite and check it on the FIDDLE engine.
I replaced the window function, I checked it in MySQL – it works, not in SQLite, tell me what else to do?
WITH p as ( WITH e AS (
SELECT s.date AS dateS, e.date AS dateE
from ev s
JOIN ev e
ON s.cid = e.cid
)
SELECT t2.`dateSS` as dateStart, t2.`dateEE` as dateEnd, count(*) all_count
FROM (
SELECT
(SELECT date FROM ev t1
WHERE t1.date < ev.date
OR t1.date = ev.date AND t1.id < ev.id
ORDER BY date DESC, ID DESC LIMIT 1 ) dateSS,
date as dateEE
FROM ev
order by date
) t2
JOIN e
ON t2.`dateSS` < e.`dateE`
AND t2.`dateEE` > e.`dateS`
GROUP BY t2.`dateSS`, t2.`dateEE`
)
(SELECT
dateStart,
dateEnd,
all_count
FROM p
WHERE all_count=(SELECT MAX(all_count) FROM p)
ORDER BY dateStart ASC LIMIT 1)
UNION
(SELECT
dateStart,
dateEnd,
all_count
FROM p
WHERE all_count=(SELECT MAX(all_count) FROM p)
ORDER BY dateStart DESC LIMIT 1);
2
Answers
In SQLite, the changes from the original MySQL query are as follows:
To convert the given code to SQLite, you need to make a few adjustments since SQLite does not support the same syntax as MySQL for some operations, such as the use of window functions. Here’s the modified code that should work in SQLite:
Please note that the code assumes you have a table named ev with columns cid, date, and id in your SQLite database. Make sure to replace ev with the actual table name in your database.
You can test this code on the SQLite Fiddle engine or any other SQLite environment to verify if it produces the desired results. Thank you!