T-SQL calculating average time

前端 未结 4 1516
走了就别回头了
走了就别回头了 2021-01-03 23:57

I have a problem with calculating average time. This is the case: i have multiple rows, in each row there is data in time format so I need to calculate an average time of al

4条回答
  •  盖世英雄少女心
    2021-01-04 00:23

    You should be able to use something similar to the following:

    select 
      cast(cast(avg(cast(CAST(times as datetime) as float)) as datetime) as time) AvgTime,
      cast(cast(sum(cast(CAST(times as datetime) as float)) as datetime) as time) TotalTime
    from yourtable;
    

    See SQL Fiddle with Demo.

    This converts the times data to a datetime, then a float so you can get the avg/sum, then you convert the value back to a datetime and finally a time

提交回复
热议问题