Group timestamp information by date and hour in Bigquery - sql

I have this timestamp metric which shows the following information:
2021-08-30 22:10:22.838 UTC
I would like to split and group this info by date and hour, so it should look something like this in BQ:
Date: 2021-08-30
Hour: 22:00:00 UTC
Anyone know how do do this?
Thanks!!

To extract the date and hour you can use this (replacing your_ts with the appropriate field name).
SELECT
EXTRACT(DATE FROM your_ts) dt,
EXTRACT(HOUR FROM your_ts) hr
FROM tbl
If you want to keep the formatting you provided (returning strings), you can try something like this.
SELECT
FORMAT_TIMESTAMP("%F", your_ts) dt,
FORMAT_TIMESTAMP("%X", TIMESTAMP_TRUNC(your_ts, HOUR))
FROM tbl

Related

Date_trunc error to truck the user activity on website

I'm using google cloud and trying to format the event_timestamp column to extract the date and time to identifying time that user take to make a purchase on website
DATE_TRUNC(DATE (event_timestamp), 'month') AS purchase_date,
as per the query, I got an error "A valid date part name is required at [6:31]"
the dateset
event_timestamp
1605430896492843
expecting results example
Purchase_date
2020-11-15 12:27:20
From the docs it seems, that it should be without quotes:
DATE_TRUNC(DATE (event_timestamp), MONTH) AS purchase_date
Change the order:
DATE_TRUNC('month', DATE (event_timestamp)) AS purchase_date
Since your event_timestamp value consists of 16 digits, you need to use the timestamp_micros function.
timestamp_micros Interprets int64_expression as the number of microseconds since 1970-01-01 00:00:00 UTC and returns a timestamp.
select timestamp_micros(event_timestamp) AS purchase_date
This will output (2020-11-15 09:01:36.492843 UTC) for the input (1605430896492843).
The date_trunc /timestamp_trunc function will truncate the date to the given date_part, so according to your expected output you don't need to use it.
select timestamp_trunc(timestamp_micros(1605430896492843), month) AS purchase_date
This will output (2020-11-01 00:00:00 UTC)

Is there a quick way to separate date and time from a time stamp in sql?

I am using sql to calculate the average daily temperature and max daily temperature based on a date timestamp in an existing database. Is there a quick way to accomplish this?
I am using dBeaver to do all my data calculations and the following code is what I have used so far:
SELECT
convert(varchar, OBS_TIME_LOCAL , 100) AS datepart,
convert(varchar, OBS_TIME_LOCAL, 108) AS timepart,
CAST (datepart AS date) date_local,
CAST (timepart AS time) time_local
FROM
APP_RSERVERLOAD.ONC_TMS_CUR_ONCOR_WEATHER;
The data format as follows:
ID time_stamp temp
--------------------------------------------
de2145 2018-07-16 16:55 103
There are multiple IDs with 24hrs of temperature data at 1 min increments.
I am not sure if I understand what you need but I will try:
Your question: "Is there a quick way to separate date and time from a time stamp in sql?"
Answer:
select to_char(datec, 'hh24:mi')
, to_char(datec, 'yyyy-mm-dd')
from test;
Use to_char with format to select date part and time part.
You seem to want aggregation:
SELECT convert(date, OBS_TIME_LOCAL) AS datepart,
avg(temp), max(temp)
FROM APP_RSERVERLOAD.ONC_TMS_CUR_ONCOR_WEATHER
GROUP BY convert(date, OBS_TIME_LOCAL);

PostgreSQL: truncate hour/min/second from a timestamp

I am using the following query to change all date to the Monday of the corresponding week:
select date_trunc('week', join_date) as join_wk from my_table
This query converts 2017-08-23 11:30:02 to 2017-08-21 00:00:00
I am wondering if it is possible to remove the hour/min/secondfrom the output 2017-08-21 00:00:00? i.e. make the output in the format of 2017-08-21
date_trunc returns a timestamp. You could cast it to a date to lose the time part of it:
SELECT DATE_TRUNC('week', join_date)::DATE AS join_wk FROM my_table
-- Here ----------------------------^

How to get the date and time from timestamp in PostgreSQL select query?

How to get the date and time only up to minutes, not seconds, from timestamp in PostgreSQL. I need date as well as time.
For example:
2000-12-16 12:21:13-05
From this I need
2000-12-16 12:21 (no seconds and milliseconds only date and time in hours and minutes)
From a timestamp with time zone field, say update_time, how do I get date as well as time like above using PostgreSQL select query.
Please help me.
There are plenty of date-time functions available with postgresql:
See the list here
http://www.postgresql.org/docs/9.1/static/functions-datetime.html
e.g.
SELECT EXTRACT(DAY FROM TIMESTAMP '2001-02-16 20:38:40');
Result: 16
For formatting you can use these:
http://www.postgresql.org/docs/9.1/static/functions-formatting.html
e.g.
select to_char(current_timestamp, 'YYYY-MM-DD HH24:MI') ...
To get the date from a timestamp (or timestamptz) a simple cast is fastest:
SELECT now()::date
You get the date according to your local time zone either way.
If you want text in a certain format, go with to_char() like #davek provided.
If you want to truncate (round down) the value of a timestamp to a unit of time, use date_trunc():
SELECT date_trunc('minute', now());
This should be enough:
select now()::date, now()::time
, pg_typeof(now()), pg_typeof(now()::date), pg_typeof(now()::time)

calculate a sum of type time using sql

How to caculate sum of times of my colonne called "timeSpent" having this format: HH:mm
in SQL? I am using MySQL.
the type of my column is Time.
it has this structure
TimeFrom like 10:00:00 12:00:00 02:00:00
TimeUntil 08:00:00 09:15:00 01:15:00
Time spent
total time 03:15:00
SELECT SEC_TO_TIME( SUM( TIME_TO_SEC( `timeSpent` ) ) ) AS timeSum
FROM YourTableName
100% working code to get sum of time out of MYSQL Database:
SELECT
SEC_TO_TIME( SUM(time_to_sec(`db`.`tablename`)))
As timeSum
FROM
`tablename`
Try and confirm.
Thanks.
In MySQL, you would do something like this to get the time interval:
SELECT TIMEDIFF('08:00:00', '10:00:00');
Then to add the time intervals, you would do:
SELECT ADDTIME('01:00:00', '01:30:00');
Unfortunately, you're not storing dates or using 24-hour time, so these calculations would end up incorrect since your TimeUntil is actually lower than your TimeFrom.
Another approach would be (assuming you sort out the above issue) to store the time intervals as seconds using TIMESTAMPDIFF():
UPDATE my_table SET time_spent=TIMESTAMPDIFF(start, end));
SELECT SEC_TO_TIME(SUM(time_spent)) FROM my_table;
If the data type of the timeSpent column is TIME you should be able to use the following query:
SELECT SUM(timeSpent)
FROM YourTableName -- replace YourTableName with the actual table name
However, if that is not the case, you may have to use a cast to convert to the Time data type. Something like this should work:
SELECT SUM(timeSpent - CAST('0:0:0' as TIME))
FROM YourTableName -- replace YourTableName with the actual table name