SELECT query to fetch records from table since last fetch based on last updated time - sql

I want to create a select query that fetches 100 records from a table based on the last updated field.
SELECT *
FROM table
WHERE lastupdated > 'xxxx'
ORDER BY lastupdated ASC
LIMIT 100
I will then want to run this query again later with the last updated time from the last record.
What if the 101st record had the same lastupdated value as the 100th record in the first set? This will lead to the next fetch missing that record right?
How do I ensure that I don't duplicate or miss records in the next fetch? Would adding an id column and filtering by id as well solve the issue?

Related

Select the last two row from a table and storing them in another table in SQL

I have a table which is updated with new data every 15 seconds in time and I want to select the last tow row (the new data stored in the table) and save it in another table every time get updated (the new table have only two row which can replace every time).
I try to select the last data but I don't know how to replace it every 15 second in the second table.
SELECT TOP 20 [Index]
,[SourceIndex]
,[Timestamp]
,[CounterID]
,[Value]
,[Name]
FROM [BDCS].[dbo].[Vcd.CounterValues]
ORDER BY 'Index' desc;
Can someone please tell me how to do that?

SQL how to select date with limit?

I have a limited history table. It uses composite keys - one of the columns is the timestamp in long.
I want to select/delete rows that are the oldest, and keep the latest 1000 by checking the timestamp. How do I write the statement for such a case?
You can fire delete query escaping 1000 records.
Not in a query will be scape 1000 latest records and rest will be deleted.
Delete from table where Id not in(select top 1000 Id from table order by date desc )

microsoft sql - how select data with random sorting

I have around 50k records in the table, I want to save all the records with random order and grouping into other table. eg: at the first run, system will select 10(random) records and insert into the 2nd table, at the second run, system will select 15(random) records and insert into the 2nd table & ...
until all the records moved to 2nd table.
I tried to use order by ABS(Checksum(NewID()) % XXX) to make the random order, but how can I make the grouping to control the minimum records & maximum records in the select query??
user order by NEWID() at the end
like
select * from table order by newid()

DB2 - select last inserted 5 rows from a table

I have a table that has no indexed rows, nor a specific column...
Let's say "City, PersonName, PersonAge". I need to obtain the last 5 people inserted in that table...
How can I do it in in DB2?
I tried
select * from PEOPLE fetch first 5 rows only
this work perfectly... but no idea how to do it with the LAST rows....
You can't select the last 5 rows inserted, the database doesn't keep track of this. You need some sort of autoincremented ID or timestamp and order by that column descending.

How can I retrieve just the last two rows inserted in an SQLite database?

I want to retrieve the last 2 rows inserted into an SQLite database for display in a label. What would be the proper SQLite statement to retrieve these rows?
If you last inserted data has just inserted (maybe in the same method), you can use:
last_insert_rowid
If you like to read the last insert data after, let's say a app re/start, then you need to do some ordering.
If you have a auto-incrementing id row, you can do the following
"SELECT * from your_table ORDER BY your_autoinc_id DESC LIMIT 2"