Getting records between current date and next x days in Presto - sql

Hope someone can help me out. I'm trying convert a line of SQL to work with Presto. Currently in SQL I do the following to get all records that are due in the next 0-5 days:
((EventStartDate)between getdate()-1 and dateadd(day, 5, getdate()))
I thought it would be something like this in Presto
EventStartDate between current_date and interval '5' day
But get the following error in AWS Athena: Cannot check if date is BETWEEN date and interval day to second
Thanks,
Mark

Interval needs a date or timestamp to be used and BETWEEN can only be made between to equal entities to dates twp timestamps two numbers
So do this instead
EventStartDate between current_date and current_date + interval '5' day

Related

Getting interval Monday-Sunday in SQL BIGQUERY

I'm trying to get the data between last weeks monday and last week sunday. I'm having trouble with getting the relative part. I'm trying like this:
where date <= LASTWEEKSUNDAY OR date >= LASTWEEKMON
The closest I got to what I seek was using now(), but it returned also some days from the current week. Thanks in advance
You are describing:
where date >= date_sub(date_trunc(current_date, week(Monday), interval 1 week) and
date < date_trunc(current_date, week(Monday))
Although the function calls change, the same logic works on datetimes and timestamps.
Of course week(Monday) is the default for isoweek, so you can use:
where date >= date_sub(date_trunc(current_date, isoweek, interval 1 week) and
date < date_trunc(current_date, isoweek)
I think it's what you want
where date between DATE_SUB(DATE_TRUNC(CURRENT_DATE(), WEEK(SUNDAY)), interval 6 day) and DATE_TRUNC(CURRENT_DATE(), WEEK(SUNDAY))
You shouldn't use OR in the where statement, it'll cover all the days if you use OR. Instead, you can prefer using AND or between.

count the number of days of current month from day 1 until yesterday

I am trying to calculate the number of days of the current of month from day 1 until yesterday without the need of changing the count manually. The original SQL as below:
select order_id
from orders
where date > dateadd(-23 to current_date) and date < 'today'
the desired code is something like
select order_id
from orders
where date > dateadd(datediff(day,firstdayofthemonth,current_date) to current_date) and date < 'today'
Appreciate any help
In firebird you could do:
WHERE
date >= DATEADD(1 - EXTRACT(DAY FROM CURRENT_DATE) DAY TO CURRENT_DATE)
AND date < CURRENT_DATE
In addition to the answer provided by Mark, you can also use BETWEEN (starting with Firebird 2.0.4)
WHERE
date BETWEEN current_date - extract(day from current_date) + 1
AND current_date - 1
P.S. all those answers rely upon DATE data type (thus, date column and CURRENT_DATE variable) having no time part. Which is given for modern SQL dialect 3. But if Dialect 1 would get used it is not given.
https://firebirdsql.org/file/documentation/reference_manuals/fblangref25-en/html/fblangref25-commons-predicates.html
https://firebirdsql.org/file/documentation/reference_manuals/fblangref25-en/html/fblangref25-background.html#fblangref25-structure-dialects
In addition to the answer provided by GMB, you can also use fact that Firebird allows addition of days to a date without needing to use dateadd:
date > current_date - extract(day from current_date)
and date < current_date

Trying to get the first day of last month, need a Postgresql implementation

I need to calculate the first day of last month, and the last day of last month as part of a SQL query, I found the exact answer to what I am looking for in this post
for instance
select DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0) --First day of previous month
While I can follow the logic and it seems to work in SQL Server, I am using Postgresql/Redshift and I am getting the error
[42883][500310] [Amazon](500310) Invalid operation: function
pg_catalog.date_diff("unknown", integer, timestamp without time zone) does
not exist;
Can someone explain to me why Postgresql is throwing an error and how I can modify the code to get the same solution in Postgres?
Simpler in Postgres: Use date_trunc() to get the first day of the month, and then ...
subtract "one month" for the first day of the last month.
or subtract "one day" for the last day of the last month.
SELECT date_trunc('month', now()) - interval '1 month' AS last_month_first_day
, date_trunc('month', now()) - interval '1 day' AS last_month_last_day;
Returns timestamp or timestamptz, depending on input. timestamptz for now() as input.
To return type date:
SELECT (date_trunc('month', now()) - interval '1 month')::date AS last_month_first_day
, (date_trunc('month', now()))::date - 1 AS last_month_last_day;
You can subtract integer from a date (but not from a timestamp) to subtract days.
db<>fiddle here
Related:
How do I determine the last day of the previous month using PostgreSQL?
How to get the end of a day?
Second edit: okay I finally have a solution. This works in postgresql
BETWEEN DATEADD(days, (DATEPART(day, CURRENT_DATE) - 1), DATEADD(month,
-1, CURRENT_DATE)) AND DATEADD(days, (DATEPART(day, CURRENT_DATE) - 1),
CURRENT_DATE)
I figured it out. You can't put an int in the date field of a datediff or dateadd in Postgresql. If you take the SQL Server solution from above and replace the 0's in each function with two of the same date, it doesn't matter what date it is but they have to be the same, it will produce the desired output.
edit: Actually this only helps get the first of last month, not the last of last month, as I haven't figured out a way to replicate the purpose of the -1 included in the second SQL script in the link above. I can't replace this int with an equivalent date. Help would still be appreciated.

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?

How to display SQL dates in the last 30 days?

I want to display the dates only in the past 30 days. for my SQL command. I have a DATETIME field called statement_to_date and I want to find all of the columns in the past 30 days for statement_to_date. Here's what I have so far:
SELECT Statement_TO_DATE, STATEMENT_FROM_DATE
FROM claim;
SELECT DATE_ADD(NOW(), INTERVAL -30 DAY)
I thought I could plug Statement_TO_DATE where INTERVAL is, but it's not working. Any ideas?
You are missing the where clause:
SELECT Statement_TO_DATE, STATEMENT_FROM_DATE
FROM claim
WHERE statement_to_date >= DATE_ADD(NOW(), INTERVAL -30 DAY); -- assumes the value is never in the future
Normally, when working with timespans in dates, you don't want the time component of the current date. So, this is more typical:
SELECT Statement_TO_DATE, STATEMENT_FROM_DATE
FROM claim
WHERE statement_to_date >= DATE_ADD(CURDATE(), INTERVAL -30 DAY);
Note that MySQL also has DATE_SUB(), if you don't want a negative time interval.
From w3schools you can see that you're not using DATE_ADD() correctly.
Also like Gordon Linoff said you're missing a WHERE clause, try:
SELECT Statement_TO_DATE, STATEMENT_FROM_DATE
FROM claim
WHERE statement_to_date >= DATE_ADD(day, -30, GETDATE());
Select DATEADD(Month, -1, getdate())