比如几个时间
insert into t values ('刘备','07:30:00')
insert into t values ('刘备','06:30:00')
insert into t values ('刘备','06:30:00')
insert into t values ('刘备','06:30:00')
insert into t values ('刘备','06:30:00')
insert into t values ('刘备','08:20:00')
insert into t values ('刘备','09:15:00')
求得平均时间
with cte as
(select name,
avg(cast(substring(uptime,1,2) as numeric)*60+cast(substring(uptime,4,2) as numeric)) uptime
from t
group by name)
select name,substring(convert(varchar,dateadd(n,uptime,CAST(('2000-01-01 00:00:00') as datetime)),120),12,8) from cte
with cte是临时表
avg(cast(substring(uptime,1,2) as numeric)60+cast(substring(uptime,4,2) as numeric))
截取一段60都转换为分钟 算平均值
substring(convert(varchar,dateadd(n,uptime,CAST((‘2000-01-01 00:00:00’) as datetime)),120),12,8)
拼接字符串 从00:00:00:00换成平均时间