Access 2013 Sum time values

2019-08-15 19:02发布

问题:

How to make a sum of time values in Access 2013?

My column looks like this: (thanks to @HansUp user!)

SELECT t.[Time From],
       t.[Time Until],
       Format((t.[Time Until] - t.[Time From]), 'h:nn') AS [Total],
       Format(#08:00# - (t.[Time Until] - t.[Time From]), 'h:nn') AS [Missing]
FROM tblVolkan AS t;

How can I make a sum of Missing without getting Error?

回答1:

You just sum the time spans:

SELECT 
    Sum([Time Until] - [Time From]) AS [SumTotal],
    Sum(#08:00# - ([Time Until] - [Time From])) AS [SumMissing]
FROM 
    tblVolkan;

If totals can exceed 24 hours, and you wish an hour:minute display, use the function below:

SELECT 
    FormatHourMinute(Sum([Time Until] - [Time From])) AS [SumTotal],
    FormatHourMinute(Sum(#08:00# - [Time Until] - [Time From])) AS [SumMissing]
FROM 
    tblVolkan;

-

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