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

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')

Related

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

How to query for "Yesterday, in my timezone" when all my timestamps are UTC

I am trying to include, in my where clause, a way to automatically pull data for the previous day based on my current timezone. All of my data is stored with a UTC timestamp.
I can change my timestamp from UTC to my timezone ("America/Chicago") and I can automatically pull data for the last X days; for example, for the prior week, without having to manually enter a date. But I cannot figure out how to do both simultaneously in my where clause.
SELECT *
FROM `my-data-set`
WHERE
event_time >= CAST(DATE_SUB(CURRENT_DATE(), INTERVAL 7 DAY) AS TIMESTAMP)
AND event_time < CAST(DATE_SUB(CURRENT_DATE(), INTERVAL 0 DAY) AS TIMESTAMP)
I would like to be able to look at the previous week or day in my timezone, not the previous UTC day.
You can do specify the timezone:
where date(event_time, 'America/Chicago') = date_add(current_date, interval -1 day)
Note that the parentheses are not needed for current_date.
The key to the logic is converting the UTC timestamp to a date in your local time.

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

Between Operator Big Query Standard SQL

Using Standard SQL in BQ - as part of a task I want to search for records created between 2pm the previous day & 2pm on current day
I have found
SELECT DATETIME_SUB(DATETIME_TRUNC(CURRENT_DATETIME(), DAY), INTERVAL 10 hour) Gives me 2PM yesterday
SELECT DATETIME_ADD(DATETIME_TRUNC(CURRENT_DATETIME(), DAY), INTERVAL 14 hour)
Gives me 2pm today
So, i assumed i could use this in my query
Select * from
TableA
where CreatedDate Between
DATETIME_SUB(DATETIME_TRUNC(CURRENT_DATETIME(), DAY), INTERVAL 10 hour) and DATETIME_ADD(DATETIME_TRUNC(CURRENT_DATETIME(), DAY), INTERVAL 14 hour)
However I get the following
No matching signature for operator BETWEEN for argument types:
TIMESTAMP, DATETIME, DATETIME. Supported signature: (ANY) BETWEEN
(ANY) AND (ANY)
Where am i going wrong?
Your issue is that CreatedDate is TIMESTAMP and you need to convert into a DATETIME
It could be like:
where DATETIME(CreatedDate) Between ...
But you could easily write your own statements for TIMESTAMP
SELECT timestamp_sub(timestamp_trunc(current_timestamp() ,
DAY),interval 10 hour)

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?