问题
I have a table trans_hist in MS Access program where time is stored in short Date format "HH:MM" ex:
[![Image][1]][1]
Now I have created a query which tells total time for each user (Just simply summing the time) but if the total time exceeds 24 hours then it resets and starts over again from 00:15(all time are stored in 15 minutes intervals)
Problem: For Customer(UID) 1 the total time should have been 32:30 however it shows 8:30
Current Result:
UID Time_Elapsed
1 5:00
1 8:30
1 9:00
1 6:00
2 2:15
2 2:00
3 1:15
5 4:00
1 4:00
Result:
[![Image][2]][2]
- DATA
Cust_UID Trans_Date Agen_Name Prog_Name Prime_Serv Prime_Serv_Time 10014 13-Dec-15 LAC RA BMC 01:00 10021 14-Dec-15 LAC RA AP 01:00 10022 15-Dec-15 LAC RA AP 01:00 10021 16-Dec-15 LAC RA SM 00:45 10020 17-Dec-15 LAC RA AP 01:00 10027 18-Dec-15 LAC RA DA 00:15 10028 18-Dec-15 LAC RA DA 00:15 10026 18-Dec-15 LAC RA DA 00:15 10029 18-Dec-15 LAC RA DA 00:15 10030 18-Dec-15 LAC RA DA 00:15 10031 18-Dec-15 LAC RA DA 00:15 10023 19-Dec-15 LAC RA Clinical 02:00 10023 20-Dec-15 LAC RA Clinical 01:30 10023 20-Dec-15 LAC RA Clinical 02:00 10020 21-Dec-15 LAC RA SM 00:15 10023 21-Dec-15 LAC RA SM 00:30 10022 22-Dec-15 LAC RA Clinical 00:30 10022 22-Dec-15 LAC RA IB 00:30 10021 22-Dec-15 LAC RA IB 00:30 10009 22-Dec-15 LAC RA IB 00:30 10019 23-Dec-15 LAC RA STM 00:45 10009 23-Dec-15 LAC RA Staff - In 00:30 10021 23-Dec-15 LAC RA Staff - In 00:30 10022 23-Dec-15 LAC RA Staff - In 00:30 10024 23-Dec-15 LAC RA Staff - In 00:30 10033 23-Dec-15 LAC RA Staff - In 00:30 10025 23-Dec-15 LAC RA Clinical 00:45 10035 28-Dec-15 LAC OA CA 05:00 10040 28-Dec-15 LAC OA CA 05:00 10039 28-Dec-15 LAC OA CA 05:00 10038 28-Dec-15 LAC OA CA 05:00 10042 28-Dec-15 LAC OA CA 05:00 10036 28-Dec-15 LAC OA CA 05:00 10037 28-Dec-15 LAC OA CA 05:00 10006 30-Dec-15 LAC Test 1 DA 01:45 10005 30-Dec-15 LAC Test 2 DG 01:45 10015 30-Dec-15 LAC Test 2 IB 02:15 10015 30-Dec-15 LAC Test 4 DG 03:15 10019 30-Dec-15 LAC OA CA 15:30 10005 31-Dec-15 LAC OA CA 12:00
[Data][3]
Result
Prog_Name Prime_Serv Total_Serv_Time OA CA 62:30 RA AP 3:0 RA BMC 1:0 RA Clinical 7:45 RA DA 2:30 RA IB 2:30 RA Staff - In 2:30 RA SM 2:30 RA STM 1:45 Test 1 DA 2:45 Test 2 DG 2:45 Test 2 IB 2:15 Test 4 DG 3:15
[Result][4]
回答1:
I hope, this SQL query helps:
SELECT T.UID,
CSTR(total_hours
+ INT(total_min / 60))
+ ":"
+ CSTR(total_min Mod 60) as result
FROM
(SELECT UID,
SUM(HOUR(TH.time_elapsed)) AS total_hours,
SUM(MINUTE(TH.time_elapsed)) AS total_min
FROM trans_hist AS TH
GROUP BY UID) AS T
回答2:
Use a function like this:
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
and a simple query:
Select
UID,
FormatHourMinute(Sum([Time_Elapsed])) As TotalTime
From
trans_hist
Group By
UID
回答3:
Try this.
SELECT UID, switch (
t>1 , '00:15'
,true,format(t,'hh:mm')
) as Time_Elapsed from
(
SELECT UID, sum(Time_Elapsed) as t
From Table1
Group By UID
)
来源:https://stackoverflow.com/questions/34542819/ms-access-summing-time-greater-than-24-hours-in-query-restarts-from-1-again