[英]Using DATEPART function in access SQL query with joined tables and aggregate function
[英]Time Query with DatePart function
根据我的阅读,我创建了以下查询,以按时间,按秒,然后将它们转换为hh:mm:ss格式对所有内容进行分组:
SELECT Exits.Categorization, Format(Sum(DatePart("h",[Time_after_Search])*3600+DatePart("n",[Time_after_Search])*60)) AS TotalSeconds, Round([TotalSeconds]/3600,0) & ':' & Right("00" & Round(((TotalSeconds/3600) + Round(TotalSeconds/3600,0))*60,0),2) AS TotalTime
FROM Exits
GROUP BY Exits.Categorization;
对于某些分组,非常奇怪的是,秒和hh:mm:ss之间的转换是完全正确的。 但是对于其他组,我注意到当您将秒转换为hh:mm:ss格式时,它最多会延迟1200秒。 为什么在世界上会发生这种情况? 这是我查询的结果,因此您可以看到。 例如,如果您看到第一类,则CAS号正常工作。 但是化学药品应该产生204360而不是203160(我的意思是相当于小时),依此类推
Categorization TotalSeconds TotalTime
1080 0:18
Brand Name 74880 21:08
CAS Number 37200 10:20
Catalog Numbers 522960 145:16
Chemical 203160 56:46
Generic Term 34860 10:81
Mistake??? 0 0:00
Product Characteristic 254640 71:04
Product Type 382560 106:36
Supplier Name 55560 15:26
Supplier Part Number 992160 276:96
使用如下函数:
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
Exits.Categorization,
Sum(DateDiff("s", #00:00#, [Time_after_Search])) AS TotalSeconds,
FormatHourMinute(Sum([Time_after_Search])) AS TotalTime
FROM
Exits
GROUP BY
Exits.Categorization;
也许:
SELECT
Exits.Categorization,
DateDiff("s", #00:00#, Sum([Time_after_Search])) AS TotalSeconds,
FormatHourMinute(Sum([Time_after_Search])) AS TotalTime
FROM
Exits
GROUP BY
Exits.Categorization;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.