Skip to Main Content
  • Questions
  • query records with timestamp gap 15 minute or more

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Dipak.

Asked: March 06, 2016 - 9:00 am UTC

Last updated: March 07, 2016 - 12:03 am UTC

Version: 11

Viewed 50K+ times! This question is

You Asked

We are storing data in every 30 seconds. No we need to query records for every 15 minutes after a specified timestamp.

and Connor said...

select *
from my_table
where my_column > systimestamp - numtodsinterval(15,'MINUTE')


or

select *
from my_table
where my_column > timestamp '2016-01-01 13:34:00' - numtodsinterval(15,'MINUTE')


etc etc

Remember, you'll only see committed data, so if someone does:

insert 9am
<waits 30mins>
commit;

Then its likely you will *never* see that row, because you didnt see it at 9:15am, and at 9:30 its now too old.

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Dipak Pradhan, March 06, 2016 - 1:37 pm UTC

When i am running the below given query no output comes. I need record of onday in every 15 minutes.
query
==========
select *
from DEVICE_POSITION_DATA dpd
where dpd.device_id = 795
and dpd.creation_date > systimestamp - numtodsinterval(15,'MINUTE')
and dpd.creation_date > sysdate - 1
order by dpd.creation_date desc
Connor McDonald
March 07, 2016 - 12:03 am UTC

SQL> select systimestamp from dual;

SYSTIMESTAMP
-------------------------------------
07-MAR-16 08.02.41.691000 AM +08:00

SQL> create table DEVICE_POSITION_DATA ( device_id int, creation_date timestamp );

Table created.

SQL>
SQL> insert into DEVICE_POSITION_DATA
  2  select 795, sysdate - rownum/1440
  3  from dual
  4  connect by level <= 100;

100 rows created.

SQL>
SQL> commit;

Commit complete.

SQL>
SQL> select *
  2  from DEVICE_POSITION_DATA dpd
  3  where dpd.device_id = 795
  4  and dpd.creation_date > systimestamp - numtodsinterval(15,'MINUTE')
  5  and dpd.creation_date > sysdate - 1
  6  order by dpd.creation_date desc ;

 DEVICE_ID CREATION_DATE
---------- ---------------------------------------------------------------------------
       795 07-MAR-16 08.01.15.000000 AM
       795 07-MAR-16 08.00.15.000000 AM
       795 07-MAR-16 07.59.15.000000 AM
       795 07-MAR-16 07.58.15.000000 AM
       795 07-MAR-16 07.57.15.000000 AM
       795 07-MAR-16 07.56.15.000000 AM
       795 07-MAR-16 07.55.15.000000 AM
       795 07-MAR-16 07.54.15.000000 AM
       795 07-MAR-16 07.53.15.000000 AM
       795 07-MAR-16 07.52.15.000000 AM
       795 07-MAR-16 07.51.15.000000 AM
       795 07-MAR-16 07.50.15.000000 AM
       795 07-MAR-16 07.49.15.000000 AM
       795 07-MAR-16 07.48.15.000000 AM

14 rows selected.


Send me your testcase like the one above so we can see where things are different in your environment