简体   繁体   中英

How to write a query to fetch data from exactly 25 mins back to 15 mins back? Oracle sql

I have a query like below;

SELECT fielda,fieldb,fieldc  
from mytable 
where timefield between to_date((to_char(sysdate-1/24, 'DD-MON-YYYY HH24') || ':00:00'), 'DD-MON-YYYY HH24:MI:SS') 
                    and to_date((to_char(sysdate-1/24, 'DD-MON-YYYY HH24') || ':59:59'), 'DD-MON-YYYY HH24:MI:SS');

This query takes value from the 0 th and 59 th minute of hour before previous hour. What I want is, I want to run a query, which runs on 10th minute. The query should fetch rows from my table, where time is between 25 minures and 15 minutes related to the current. For example, I run the query in 09:10:00 and some seconds. It should fetch rows where timefield is between 08:50 and 08:59:00 (i want to include data in 8:50 and 8:59). How can I do this?

You'd truncate SYSDATE to the minute and subtract the desired interval. Use >= and < in order to deal with seconds and fractions thereof properly.

select fielda, fieldb, fieldc
from mytable 
where timefield >= trunc(sysdate, 'minute') - interval '25' minute
  and timefield <  trunc(sysdate, 'minute') - interval '20' minute;
SELECT *
FROM mytable 
WHERE timefield >= sysdate - (25/1440)
  AND timefield <= sysdate - (10/1440)

24*60 = 1440

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