I have the following object:
CREATE TABLE IF NOT EXISTS {schemaName}.{tableName}
(
id VARCHAR NOT NULL,
ticker VARCHAR(16) NOT NULL,
interval VARCHAR(3) NOT NULL,
ts TIMESTAMP NOT NULL,
...
UNIQUE (ticker, interval, ts)
);
I would like to do a query that will return rows like this:
SELECT * FROM analysis
WHERE ticker = 'BTCUSDT' AND ts BETWEEN '2020-1-1' AND '2020-1-30'
ORDER BY ts;
but, for each row, adds a column that contains the timestamp of the next row (sorted by ts).
Since I've only basic knowledge of SQL, I used ChatGPT to generate a solution, but it's insanely slow (as in minutes vs. milliseconds)
SELECT t1.ts as ts_start, t2.ts as ts_end, t1.ticker, t1.interval, ...
FROM analysis t1
JOIN analysis t2
ON t1.ticker = t2.ticker AND t1.interval = t2.interval AND t1.ts < t2.ts
WHERE t1.ticker = 'BTCUSDT' AND t1.ts BETWEEN '2020-1-1' AND '2020-1-30'
ORDER BY t1.ts;
What would be a better way to make this happen?