Selecting rows created up to 30 seconds ago in Postgres - sql

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.

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;

Presto TIMESTAMP get data from 2 days ago without inputting year month date?

My goal is to have the query grab data from 2 days ago. I don't want to have to keep inputting the date like this:
WHERE usage_start_date
BETWEEN TIMESTAMP '2020-09-09 00:00:00.000' and TIMESTAMP '2020-09-09
23:59:59.999'
but instead something like:
usage_start_date = current_date - interval '2' day
the above works for my Athena Presto SQL query, but for some reason will not give all the data that ran in those 24 hours, instead giving about half the day. Is there a way to do a statement like this one to ensure it gives ALL data in that day?
WHERE current_date - interval '2' day AND
BETWEEN TIMESTAMP '00:00:00.000' and TIMESTAMP '23:59:59.999'
without inputting the year, month, day? It seems like TIMESTAMP needs the y/m/d but what about doing a LIKE so it picks up the hour, minute, second but no need to put the y/m/d?
To get a timestamp for the start of the day that was two days ago you can do
DATE_TRUNC('day', NOW() - INTERVAL '2' DAY)
e.g.
WHERE usage_start_date >= DATE_TRUNC('day', NOW() - INTERVAL '2' DAY)
AND usage_start_date < DATE_TRUNC('day', NOW() - INTERVAL '1' DAY)
You can use below query to achieve the task by fetching the hour and date from the usage_start_date
select * from table where hour(usage_start_date) between 0 and 23 and current_date - interval '2' day = date(usage_start_date)
I would suggest:
WHERE usage_start_date >= CURRENT_DATE - INTERVAL '2' DAY AND
usage_start_date < CURRENT_DATE - INTERVAL '1' DAY

Finding records older than a minute

I have a users table which consists of
some columns like name,age,created_at
created_at contains time in unix timestamps format (epoch).
I basically want to create a query to show all queries that are older than 1 minute.
I tried the following SELECT * FROM users WHERE created_at > NOW() - INTERVAL '1 minutes';
But i got the following error :
operator does not exist: double precesion > timestamp with time zone
Presumably, created_at is actually a Unix timestamp. That means it is measured in seconds, so you can do:
where created_at < extract(epoch from now() - interval '1 minute')

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;

How to run a query for every date for last 3 month

I have a table(pkg_date) in redshift. I want to fetch some data for every date for the last 3 months.
Here is my query
select * from pkg_data where scan_date < current_date;
How can I use current_date as a variable in the query itself and run this query for every date from April 1.
I have set a cron job which will run in every hour. In every hour it should run with different current_date
SELECT *
FROM pkg_data
WHERE scan_date > CURRENT_DATE - INTERVAL '3 months'
Be careful — Redshift works in UTC, so the CURRENT_DATE might suffer from timezone effects and be +/- what you expect sometimes.
SELECT
CURRENT_DATE,
(CURRENT_DATE - INTERVAL '3 months')::date
Returns:
2018-06-21 2018-03-21
Also be careful with strange lengths of months!
SELECT DATE '2018-05-31' - INTERVAL '3 months'
returns:
2018-02-28 00:00:00
Notice that it gave the last day of the month (31st vs 28th).
By the way, you can use DATE '2018-05-31' or '2018-05-31'::DATE, and also INTERVAL '3 months' or '3 months'::INTERVAL to convert types.
Use dateadd() for getting date 3 moth old day and GETDATE() for get current date.
ie code will look like.
select * from pkg_data where scan_date < dateadd(month,-3,GETDATE());
for cron refer How to execute scheduled SQL script on Amazon Redshift?