简体   繁体   English

SQL AVG Function 带时间格式

[英]SQL AVG Function with with a time format

So I am using Google Big Query, and I want to find the average time for my entire column.所以我正在使用谷歌大查询,我想找到我整个专栏的平均时间。 The time is formatted as hh:mm:ss for the entire column, for example 00:19:00.整个列的时间格式为 hh:mm:ss,例如 00:19:00。

select 
 AVG(ride_length) AS average_duration
FROM `casestudy1-361603.project.DecData`

However there is an error saying, "No matching signature for aggregate function AVG for argument types: STRING".但是有一个错误说,“没有参数类型的聚合 function AVG 的匹配签名:STRING”。

This is how the column looks这是列的外观

Cast strings to interval datatype and perform an average:将字符串转换为区间数据类型并执行平均:

select avg(cast(t as interval)) as agg
from unnest(['10:20:30', '30:40:50']) as t

returns返回

agg聚合
0-0 0 20:30:40 0-0 0 20:30:40

UPD : If you want to extract specific parts of the result, you may use justify_interval to normalize interval to standard day duration (24h) and use extract function to extract portions. UPD :如果要提取结果的特定部分,可以使用justify_interval将间隔标准化为标准日持续时间(24 小时),并使用extract function提取部分。

with src as (
  select avg(cast(t as interval)) as agg
  from unnest(['70:20:30', '40:40:50']) as t
)
select
  src.agg,
  justify_interval(agg) as normalized_,
  extract(hour from justify_interval(agg)) as h
from src
agg聚合 normalized_归一化_ h H
0-0 0 55:30:40 0-0 0 55:30:40 0-0 2 7:30:40 0-0 2 7:30:40 7 7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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