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

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

Related

How to get last week's data from the previous day in postgressql?

I want to extract data for a week from previous day.
For example:
Today's date : 21-06-2021
I want a week's data from : 13-06-2021 to 20-06-2021
In Postgressql
you can try
select * from ur_tbl
where
date_clmn >= (CURRENT_DATE - INTERVAL '7 day') and
date_clmm < CURRENT_DATE

MariaDB Find the First Day of the Month and Convert to UTC

I'm using MariaDB and my records are all stored in UTC however I want to be able to find those records that are within the previous 12 calendar months based on my local timezone.
Here's the steps I took:
SELECT
CURRENT_TIMESTAMP() AS Current_Date_And_Time_In_UTC,
CONVERT_TZ(CURRENT_TIMESTAMP(),'UTC','Pacific/Auckland') AS Current_Date_And_Time_In_Auckland,
DATE(CONVERT_TZ(CURRENT_TIMESTAMP(),'UTC','Pacific/Auckland')) AS Date_In_Auckland,
LAST_DAY(DATE(CONVERT_TZ(CURRENT_TIMESTAMP(),'UTC','Pacific/Auckland')) - INTERVAL 1 MONTH) AS Last_Day_Previous_Month_In_Auckland,
LAST_DAY(DATE(CONVERT_TZ(CURRENT_TIMESTAMP(),'UTC','Pacific/Auckland')) - INTERVAL 1 MONTH) + INTERVAL 1 DAY AS First_Day_Current_Month_In_Auckland,
TIMESTAMP(LAST_DAY(DATE(CONVERT_TZ(CURRENT_TIMESTAMP(),'UTC','Pacific/Auckland')) - INTERVAL 1 MONTH) + INTERVAL 1 DAY, MAKETIME(0,0,0)) AS First_Day_At_Midnight_Current_Month_In_Auckland,
CONVERT_TZ(TIMESTAMP(LAST_DAY(DATE(CONVERT_TZ(CURRENT_TIMESTAMP(),'UTC','Pacific/Auckland')) - INTERVAL 1 MONTH) + INTERVAL 1 DAY, MAKETIME(0,0,0)), 'Pacific/Auckland','UTC') AS First_Day_At_Midnight_Current_Month_In_UTC
This lead me to using this as my range:
SELECT
CONVERT_TZ(TIMESTAMP(LAST_DAY(DATE(CONVERT_TZ(CURRENT_TIMESTAMP(),'UTC','Pacific/Auckland')) - INTERVAL 13 MONTH) + INTERVAL 1 DAY, MAKETIME(0,0,0)), 'Pacific/Auckland','UTC') AS Start_DateTime_In_UTC,
CONVERT_TZ(TIMESTAMP(LAST_DAY(DATE(CONVERT_TZ(CURRENT_TIMESTAMP(),'UTC','Pacific/Auckland')) - INTERVAL 1 MONTH) + INTERVAL 1 DAY, MAKETIME(0,0,0)), 'Pacific/Auckland','UTC') AS End_DateTime_In_UTC
Is there a simpler way to achieve the same result?
If you have a column of timestamp datatype, then no offsetting at all should be necessary.
The documentation states:
If a column uses the TIMESTAMP data type, then any inserted values are converted from the session's time zone to Coordinated Universal Time (UTC) when stored, and converted back to the session's time zone when retrieved.
Assuming that the timezone of your session is set to your local timezone, filtering the column against the last 12 months should be as simple as:
where myts >= date_format(current_date, '%Y-%m-01') - interval 12 month
and myts < date_format(current_date, '%Y-%m-01')

Oracle SQL Developer showing last 90 days of data

im trying to get a query working that will show the last 90 days of data. this is my timestamp code(it was a unix timestamp) :
"" to_date('1970-01-01','YYYY-MM-DD') +
numtodsinterval(c.f_crtm,'SECOND')- 6/24 as "oracle date" ""
this is the format of my date when the above code is run: 21-JUN-2020 15:48:59
i know it has something to do with the SYSDATE -90 but i cant figure it out.
any help would be great.
I am thinking of something like this:
(date '1970-01-01' + c.f_crtm * interval '1' second) - interval '6' hour > sysdate - interval '90' day
Or to use an index on (f_crtm), you can rearrange the logic:
c.f_crtm > (trunc(sysdate - interval '90' day + interval '6 hour') - date '1970-01-01')*24*60*60

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?

Get the timestamp of a day of the week

I am trying to find the timestamp of a certain day of the week with postgrSQL. For example, one might use now() to get the current time, but I want to get the same type of data return for, say, the most recent Monday or Sunday.
I am looking for this because I have a query which retrieves data between now and a few months ago and I don't want to include the current week. As an example what it does now:
creation_date > now() - INTERVAL '2 month'
I thought this might achieve my goals:
BETWEEN now() - INTERVAL '4 month' AND now() - INTERVAL '1 week'
But it just subtracts 7 days from the query which is not what I want. Basically, I want:
BETWEEN now() - INTERVAL '4 month' AND ????
Where the question marks are timestamp for the most recent Sunday, at least I believe that would work.
date_trunc('week', current_date) - interval '1' day
date_trunc('week', current_date) will return the date at the start of the week.
The start of the week is always Monday in Postgres. That's why you need to subract another day from the result to get the Sunday.
You can extract the current day of the week (0-6, Sunday is 0) using EXTRACT(DOW FROM date). You could then use this to create an interval to add or subtract to reach a particular weekday. For example, the most recent Sunday would be now() - (EXTRACT(DOW FROM now()) || ' days')::interval.