Generate sequence of minutes in redshift SQL - sql

How can I generate a sequence of minutes in redshift?
In postgres this will generate a sequence of minutes over the past day:
SELECT date_trunc('minute', generate_series) as minute
FROM generate_series(NOW() - '1 day'::interval, NOW(), '1 minute')
I'm not sure how to get it to work in redshift though.

generate_series() works in Amazon Redshift, as long as you don't try to join it to data in tables. This is because it runs on the Leader Node, but not on Compute Nodes.
SELECT CURRENT_DATE - generate_series(1, 60) * interval '1 minute'
Returns:
2018-07-09 23:59:00
2018-07-09 23:58:00
2018-07-09 23:57:00
...

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 create a composite datetime with fixed date + random time interval in Postgresql

I'm new to Postgresql, and I'm looking for a way to return a composite datetime, built with a fixed date, and a random time, defined as an interval.
An example:
2020-12-02 10:00:00
2020-12-02 10:10:20
2020-12-02 08:25:23
2020-12-02 09:12:11
As you can see the date is fixed, and time remains between 08:00:00 and 10:30:00.
Do you know how to replicate this behaviour using a Postgresql query?
I would suggest:
select '2020-12-02 08:00:00'::timestamp + random() * interval '2 hour 30 minute'
Or, for the current date, you could express this as:
select current_date + interval '08:00:00' + random() * interval '02:30'
Note that these illustrate two different ways of expressing an interval with 2 hours and 30 minutes (and not even giving interval '150 minute' as an example).
As a total newbie of Postgresql (but with some experience in general SQL), and reading some posts here, I've found an easy way to get it done:
SELECT
CURRENT_DATE + time '08:00:00' +
(random() * interval '3 hours')::time
AS date_wrandom_t
FROM generate_series(1, 50)
generate_series its here only to demonstrate some results, you can omit it and get a single result.
What it does:
Takes the current date (today)
Defines the starting time (min value)
Generate a random interval of 3 hours - (tested with SELECT (random() * interval '3 hours')::time)
Add the result to the minimum time (08:00:00)
Maybe it's not the best solution, but I like it as it's based on a simple syntax, easy to remember.
I wish to find some time to study this DBMS, it seems really powerful.
Any addition/suggestion is welcome!

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;

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?