DEV Community

Cover image for How to count newest entries created in an oracle database
Adrian Matei for Codever

Posted on • Edited on • Originally published at codever.dev

How to count newest entries created in an oracle database

We will base the logic around sysdate which returns the current datetime, from which we substract different units. For example for the last day from now use sysdate - 1 (units default to day) and compare with the timestamp column (in this case CREATED_AT) :

select count(*)
from PARTNER
WHERE CREATED_AT > (sysdate - 1)

-- last 2 days would be
select count(*)
from PARTNER
WHERE CREATED_AT > (sysdate - 2)
Enter fullscreen mode Exit fullscreen mode

From the last hour, respectively last two hours use the following commands, where 1/24 is the unit for hour:

-- last hour
select count(*)
from PARTNER
WHERE CREATED_AT > (sysdate - 1/24)

-- last 2 hours
select count(*)
from PARTNER
WHERE CREATED_AT > (sysdate - 2/24)
Enter fullscreen mode Exit fullscreen mode

Shared with ❤️ from Codever. 👉 use the copy to mine functionality to add it to your personal snippets collection.

Top comments (2)

Collapse
 
erikpischel profile image
Erik Pischel

"last hour" example is wrong. Should be 1/24.

Also: nice post

Collapse
 
ama profile image
Adrian Matei

Thanks Erik, missed that :)