I want to calculate average and max of the time from timestamp in Bigquery.
Timestamp-
2022-01-24T23:14:25.883233
2022-01-24T22:14:25.883233
2022-02-24T21:14:25.883233
I am expecting result of the avg of (23:14:25.883233,22:14:25.883233,21:14:25.883233) that may come around 22:something
and max of these 3 times will be 23:14:25.883233.
I tried cast and unix_timestamp function but couldn't succeed
You might consider below.
WITH sample_data AS (
SELECT ts FROM UNNEST([
TIMESTAMP '2022-01-24T23:14:25.883233', '2022-01-24T22:14:25.883233', '2022-02-24T21:14:25.883233'
]) ts
)
SELECT TIME_ADD(TIME '00:00:00', INTERVAL CAST(AVG(time_in_usec) AS INT64) MICROSECOND) AS avg_time,
TIME_ADD(TIME '00:00:00', INTERVAL MAX(time_in_usec) MICROSECOND) AS max_time
FROM (
SELECT TIMESTAMP_DIFF(ts, TIMESTAMP_TRUNC(ts, DAY), MICROSECOND) time_in_usec
FROM sample_data
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.