I have a table with 2 columns
+---------+------------+
| symbol | ts |
+---------+------------+
| 1 | 1524696300 |
| 1 | 1524697200 |
| 1 | 1524698100 |
| 1 | 1524699000 |
| 1 | 1524699900 |
| 1 | 1524700800 |
| 1 | 1524701700 |
| 1 | 1524702600 |
| 1 | 1524703500 |
| 1 | 1524704400 |
| 1 | 1524705300 |
| 1 | 1524706200 |
| 2 | 1524697200 |
| 2 | 1524698100 |
| 2 | 1524699000 |
| 2 | 1524699900 |
+---------+------------+
I want to remove older than 10 rows under each group, each row is separated by 900 seconds and may have different timestamp values at the first and last row but the difference of 900 remains constant
I tried this query
sqlite> select * from ohlc2 where ts < (select max(ts) from ohlc2) - 8100;
It only works on the table as a whole and not per group so if my item 1 and 2 have different starting and ending timestamps, the above method wont work
I am getting an error in this query which I tried now
sqlite> with m as (select symbol, max(ts) from ohlc2 group by symbol) select * from ohlc2 where symbol = m.symbol and ts < m.max - 8100;
How can I delete all rows older than 10 timestamps per group?
In SQLite, you can do this with a correlated subquery:
delete ohlc2
where ts < (select o2.ts
from ohlc2 o2
where o2.symbol = ohlc2.symbol
order by o2.ts desc
limit 1 offset 9
);
I primarily work with MS SQL Server, so I don't know if SQLLite supports this syntax or not, but if this is valid syntax in SQLLite, then it should work:
DELETE T1
FROM OHLC2 T1
INNER JOIN
(
SELECT
symbol,
MAX(ts) AS max_ts
FROM
OHLC2
) SQ ON SQ.symbol = T1.symbol AND SQ.max_ts > T1.ts + 8100
Should also work with a CTE as you have it, but you need to name your second column in the CTE with an alias.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.