How to get data from the last 5 FULL minutes? - sql

If I write:
SELECT *
FROM table
WHERE date >= NOW - INTERVAL '5 MINTUE';
I will get all the data from the last 5 minutes, but the first and last minute won't be full minutes, they'll likely be partial minutes. Meaning if "NOW()" is 4:30:24, then NOW() - INTERVAL '5 MINUTE' will get me from 4:25:24 to 4:30:24. Minute 25 is partial and only contains 36 seconds, same with minute 30 which only contains 24 seconds.
How would I go about getting the last full 5 minutes? I considered removing the first and last minute or min/max, but that idea became a challenge when I consider minute [58, 59, 60, 01, 02]. In this case min/max doesn't account for the cycle back to 0 after minute 60.
Any ideas would be helpful.

You can use date_trunc():
SELECT *
FROM table
WHERE date >= date_trunc('minute', now()) - interval '5 minute' and
date < date_trunc('minute', now())

Try something like this
SELECT *
FROM table
WHERE date >= DateADD(mi, -5, Current_TimeStamp)

Related

SQL SELECT WHERE datetime is between NOW() interval

how can I select in MariaDB between two dates (in this case between two minutes)? I mean I want to select between from now() +5 minutes and now() + 30 minutes.
I tried with this query but no luck.
SELECT req_id FROM info WHERE date(sent_date) BETWEEN (NOW() - INTERVAL 5 MINUTE) AND date(sent_date) < (NOW() - INTERVAL 30 MINUTE)
Thank you so much for helping.
BTW, I tried to search answer to my question in stackoverflow but I don't found.
The syntax of your WHERE clause is off. Use this version:
SELECT req_id
FROM info
WHERE sent_date BETWEEN NOW() - INTERVAL 5 MINUTE AND NOW() - INTERVAL 30 MINUTE;

How to subtract days to a Timestamp in CrateDB SQL query?

How can i subtract days to a timestamp in CrateDB SQL query?
Exist something similar to this?
TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 14 DAY)
Don't think there is a built in function but you could do something like this
SELECT DATE_FORMAT(CURRENT_TIMESTAMP - 1000*60*60*24*14) LIMIT 100
in this example (1000 * 60 * 60) * 24 * 14 (24 is to get days and 14 is your number of days)
NB. You can also cast dates into timestamp and perform similar functionality
SELECT ABS(cast('2019-01-1' AS TIMESTAMP) - CURRENT_TIMESTAMP ) / (1000*60*60*24) LIMIT 100
this will get you a number of days between now and 1st of January
So far that's all what they have in their docs
You can subtract INTERVAL from TIMESTAMP, but before any matematichal operation you need to CAST the datatype, you can do it in this way:
SELECT now() - CAST('14 day' AS INTERVAL)
Or the same function of above, but in a contracted way
SELECT now() - '14 day'::INTERVAL;
As a string to be CAST to an INTERVAL you can use a number followed by any of this:
second
minute
hour
day
week
month
quarter
year

PostgresSQL - the date works weird

I need create select for all rows it are older than 15 minutes at the moment.
SELECT last_answer_date FROM messages WHERE last_answer_date > NOW() - INTERVAL '15 minutes';
When I start this select now in 16:51:00 I get result:
2019-01-17 16:25:00
In other words I mean:
Give me all rows where last_answer_date > 16:51:00 - 15 minutes (because now 16:51:00)
Give me all rows `where last_answer_date > 16:36:00 (because now 16:51:00 - 15 minutes = 16:36:00)
And I get 16:25:00 but 16:25:00 < 16:36:00
EDIT: When I change > to < I get 2019-01-16 17:50:27
EDIT2: Example:
SELECT insert_date FROM smev_messages WHERE insert_date < NOW() - INTERVAL '15 minutes';
now = 17:16
change to SELECT insert_date FROM smev_messages WHERE insert_date > NOW() - INTERVAL '15 minutes';
17:17
I think it might be a problem with your timezone.
You can check it with show timezone;
Or simply go with select now() and see if it matches your expectations.
If you have a big table with a lot of data you will get some performance problems because when you call
NOW() - INTERVAL '15 minutes' internally postgress need convert your interval to a date
Postgress Interval
I think a better way to-do that you should do something like that to improve performance, because all dates are long values and you should compare it like long values.
-- 15 minutes in millis = 900000
-- 15 minutes in secods = 900
-- Retrive all data inserted at latest 15 minutes
SELECT last_answer_date FROM messages WHERE (NOW() - last_answer_date) < 900;
-- Retrive all data inserted more than 15 minutes
SELECT last_answer_date FROM messages WHERE (NOW() - last_answer_date) > 900;

SYSDATE - Timestamp to retrieve orders from last 2 minutes

This is used in a database package as a delay to pass data. The delay is 2 minutes so I need to only retrieve records if they've been in the database for 2 minutes or more.
This is what I have:
((SYSDATE - trunc(last_updated))*24*60) > l_delay_mins;
l_delay_mins = 2 minutes in this case.
However, the trunc cuts off the time and defaults to midnight, when the conversion is done it gives me the fraction of the day and then multiplies 24*60 which is always larger than 2, so the records aren't being delayed and are sending as soon as they arrive.
Use INTERVAL:
where last_updated < systimestamp - (l_delay_mins * interval '1' minute);
Here is the same thing without INTERVAL but with a minute being a fraction of the day (kind of what you tried):
where last_updated < systimestamp - (l_delay_mins / 24 / 60)
A minor amendment to the above answer
Use INTERVAL:
where last_updated <= systimestamp - (l_delay_mins * interval '1' minute);

Selecting rows created up to 30 seconds ago in Postgres

Say I want to find orders made recently. I am trying this statement:
SELECT * FROM orders WHERE time < now() - interval '30 second';
This doe not work (It does not select any row.) although the following works:
SELECT * FROM orders WHERE time < now();
Do I need to use a fixed time variable? Like:
SELECT * FROM orders WHERE time < '2018-07-01 12:00:00' - interval '30 second';
I am guessing that I should not use now() in statements because it can be changing during operations ...
I believe you have the comparison in the wrong direction:
SELECT *
FROM orders
WHERE time > now() - interval '30 second';
This returns values in the last 30 seconds.
This assumes that time is of an appropriate date type for the comparison.