Sql server 计算平均时间

比如几个时间

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换成平均时间

    原文作者:曾念念
    原文地址: https://blog.csdn.net/n20164206199/article/details/104904010
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞