0

Whats the best solution to store lap times on a SQL-Server database? the suggested format should be 01:52.525. I'm using time(3) but it does not allow me to use the Avg() Function.

Best regards,

D-Shih
  • 44,943
  • 6
  • 31
  • 51
Gamal
  • 3
  • 2
  • 1
    Could you give us more detail from your question?Provide some sample data and expect result .that really help – D-Shih Apr 28 '18 at 02:14

3 Answers3

0

Try storing the start time and duration (lap time) in epoch milliseconds, then use a SQL query (which would allow Avg()):

SELECT (duration - start) FROM lap_times (WHERE ...)

Refer to this answer for converting milliseconds to a human-readable timestamp.

SteveK
  • 995
  • 5
  • 17
0

Store the times as time. Add computed columns with the values as milliseconds. Use the computed columns for calculations.

For example:

alter table t add laptime_ms as (datediff(ms, 0, laptime));

You can now use this for averages, and convert back to time as:

select convert(time, dateadd(ms, 0, avg(laptime_ms)))
from t
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

Since you are already storing the lap duration as time, you can use DATEDIFF to calculate duration in milliseconds for the average calc and use DATEADD to convert milliseconds to time.

SELECT 
      AVG(DATEDIFF(millisecond, '', LapTime)) AS AvgLapTimeMilliseconds
    , CAST(DATEADD(millisecond, AVG(DATEDIFF(millisecond, '', LapTime)), '') AS time(3)) AS AvgLapTime
FROM dbo.Lap;
Dan Guzman
  • 43,250
  • 3
  • 46
  • 71
  • @Lamag, see [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers). – Dan Guzman Apr 29 '18 at 10:27