How do i calculate an average time using standardSQL

ぃ、小莉子 提交于 2021-01-28 12:03:03

问题


At the moment I have a column in a table with this information:

For example:

00:11:35
00:20:53    
00:17:52    
00:06:41    

And I need to display the average of that time.

These times would give an average of 00:14:15.

How to do that?

Ah, I'm trying to display this in Metabase, so I'd need a conversion form where after averaging the time it was converted to string.

So maybe it's not that simple.

The structure of field is:

Table Field: tma (type time)


回答1:


Below is for BigQuery Standard SQL

#standardSQL
WITH `project.dataset.table` AS (
  SELECT TIME '00:11:35' tma UNION ALL
  SELECT '00:20:53' UNION ALL    
  SELECT '00:17:52' UNION ALL    
  SELECT '00:06:41' 
)
SELECT 
  TIME_ADD(TIME '00:00:00', INTERVAL CAST(AVG(TIME_DIFF(tma, TIME '00:00:00', SECOND)) AS INT64) SECOND) average_time,
  FORMAT_TIME('%T', TIME_ADD(TIME '00:00:00', INTERVAL CAST(AVG(TIME_DIFF(tma, TIME '00:00:00', SECOND)) AS INT64) SECOND)) average_time_as_string
FROM `project.dataset.table`   

with result

Row average_time    average_time_as_string   
1   00:14:15        00:14:15     


来源:https://stackoverflow.com/questions/52975901/how-do-i-calculate-an-average-time-using-standardsql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!