简体   繁体   中英

How to find, average of time in BigQuery grouped by a column?

I have the below columns in table job_stats . job_end_time is TIME datatype on which BQ doesn't seem to allow usage of average function directly. I am using Standard SQL and trying to find the average end time of each job.

job_name run_date job_start_time job_end_time


A 2020-08-03 15:30:00 15:40:00

A 2020-08-10 15:40:00 16:00:00

B 2020-08-01 09:00:00 09:20:00

B 2020-08-08 09:30:00 10:00:00

Expected output:

job_name avg_end_time

A 15:50:00

B 09:40:00

Below is for BigQuery Standard SQL

#standardSQL 
SELECT job_name, 
  FORMAT_TIMESTAMP(
    '%T', 
    TIMESTAMP_SECONDS(CAST(AVG(TIME_DIFF(job_end_time, '00:00:00', SECOND)) AS INT64))
  ) AS avg_end_time 
FROM `project.dataset. job_stats`
GROUP BY job_name   

if to apply to sample data from your question - output is

Row job_name    avg_end_time     
1   A           15:50:00     
2   B           09:40:00     

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM