简体   繁体   中英

How to fetch record using SQL in five minutes window

I am writing a SQL query which will run once a day it will execute every 5 minutes and load data from 24 hours ago in that 5 minute window. The important thing is it should not have records which is fetched in previous 5 minutes window.

I have started writing the query:

SELECT
    Qn.Name, Qn.Email, Qn.Price 
FROM
    Quotation Qn
INNER JOIN 
    Product P ON P.ProductId = Qn.ProductId
WHERE
    Qn.ProductId = 10 
    AND Qn.Date = DATEADD(D, -1, CONVERT(DATE, GETDATE()))
    AND Qn.Bname= 'CaraVan'

Thanks in advance for the help

You could create a table to store the fetched rows (or their primary key set if any) and hence to exclude the rows already fetched (contained in this new table). I would delete the rows of the new table older that 24 hours to make the process more efficient in the long run. You could to this in the same scheduled task you are using. Please let me know if you need some example code.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM