I'm trying to get the the ten latest entries in my sqlite database. I need to fetch only one entry at a time instead of those ten entries at once.
Until now, i was doing this:
"SELECT " + sql + " FROM " + TABLE_NAME
+ " WHERE _id = (SELECT max(_id)-2 FROM " + TABLE_NAME + ")";
For every entry i need, i was just doing ...max(_id)-x
but this does not work, if some entries are deleted at some time. The id's have some gaps (like 1, 2, 4, 8, ...).
So after some research i was trying something like this:
"SELECT * FROM (SELECT " + sql + ", rank() over (order by max(_id) desc) rk
FROM " + TABLE_NAME + " GROUP BY " + sql + ") t WHERE rk = 2"
this approach however crashes with the following message
android.database.sqlite.SQLiteException: near "(": syntax error (code 1): , while compiling: select * from (select price, rank() over (order by max(_id) desc) rk from refuels group by price) t where rk = 2
How can I get this done? Thanks in advance