如何计算Microsoft Access中的平均调用时间

时间:2015-12-08 20:57:08

标签: ms-access time average calculated-field

我的数据库中有一个字段,专家花在电话上的总时间,以及另一个用于拨打电话的字段。我正在尝试计算平均呼叫持续时间,但Access不会以hh:mm:ss格式返回数字而不进行某些编码。我很擅长在VBA写东西。我尝试将“平均呼叫持续时间”字段中的格式更改为“长时间”,但它显示为一天中的某个时间,而不是平均持续时间,还是我看错了?

是否有其他人遇到类似的问题,如果有,你是如何解决它的?

任何帮助将不胜感激

谢谢!

2 个答案:

答案 0 :(得分:0)

如果使用数据类型日期记录时间,您可以使用查询:

Select CDate(Avg([TimeSpent])) As TotalTimeSpent
From YourTable

您可能需要将字段的格式指定为:h:nn

如果你的小时数超过24小时,你将不得不使用一个函数创建一个hh:nn阅读:

Public Function FormatHourMinute( _
  ByVal datTime As Date, _
  Optional ByVal strSeparator As String = ":") _
  As String

' Returns count of days, hours and minutes of datTime
' converted to hours and minutes as a formatted string
' with an optional choice of time separator.
'
' Example:
'   datTime: #10:03# + #20:01#
'   returns: 30:04
'
' 2005-02-05. Cactus Data ApS, CPH.

  Dim strHour       As String
  Dim strMinute     As String
  Dim strHourMinute As String

  strHour = CStr(Fix(datTime) * 24 + Hour(datTime))
  ' Add leading zero to minute count when needed.
  strMinute = Right("0" & CStr(Minute(datTime)), 2)
  strHourMinute = strHour & strSeparator & strMinute

  FormatHourMinute = strHourMinute

End Function

然后调整查询:

Select FormatHourMinute(CDate(Avg([TimeSpent]))) As TotalTimeSpent
From YourTable

答案 1 :(得分:0)

A time field in access is intended to store points of time - not durations. Although you may have chosen "Long Time" as displayformat, access still stores this as a specific date. You can try this out when you format a datetime field as longtime, enter some data into it and then change the format to tt.mm.jjjj hh:nn:ss it will give you some time on 30th dec 1899 - the starting date of access-time.

And if you create a query that sums times of the type "long time" and put tt.mm.jjjj hh:nn:ss into the format box, it will show you a later date as soon as the sum goes higher than 24 hours.

So to work with durations you normally store everything in the smallest precision you need (hours, minutes, seconds, centuries) in a numbers field and calculate everything from there. It will be much easier (and safer) to input data in a form than directly into the data-table so you can use time fields that convert the duration automatically.