MS Access按小时计算秒数

时间:2016-07-01 13:26:59

标签: ms-access

我在Access数据库中有一个表,其中唯一的信息是:

start_time (date & time);
end_time (date & time);
duration (int, seconds);

我的输出中需要的是每小时花费的时间。

一个例子:

start_time: 2016/07/01 09:00:00
end_time: 2016/07/03 09:00:00

在此示例中,假装输出为:

Date      | Hour | Duration
2016/07/01   9     3600
2016/07/01   10    3600
2016/07/01   11    3600
2016/07/01   12    3600
   ...       ...    ...
2016/07/03   8     3600

提前致谢。

2 个答案:

答案 0 :(得分:2)

如果您只计算整个小时,则会执行以下操作:

PARAMETERS 
    StartDate DateTime, 
    EndDate DateTime;
SELECT 
    DateValue(DateAdd("h",10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10),[StartDate])) AS [Date], 
    Hour(DateAdd("h",10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10),[StartDate])) AS [Hour], 
    DateDiff("s",
        DateAdd("h",10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10),[StartDate]),
        DateAdd("h",10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10)+1,[StartDate])) AS Duration
FROM 
    MsysObjects AS Uno, 
    MsysObjects AS Deca
GROUP BY 
    DateValue(DateAdd("h",10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10),[StartDate])), 
    Hour(DateAdd("h",10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10),[StartDate])), 
    DateDiff("s",
        DateAdd("h",10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10),[StartDate]),
        DateAdd("h",10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10)+1,[StartDate])), DateAdd("h",10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10),[StartDate])
HAVING 
    DateAdd("h",10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10),[StartDate])<=[EndDate];

修改

您可以直接包括您的表格(持续时间):

SELECT 
    DateValue(DateAdd("h",10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10),[StartDate])) AS [Date], 
    Hour(DateAdd("h",10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10),[StartDate])) AS [Hour], 
    DateDiff("s",
        DateAdd("h",10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10),[StartDate]),
        DateAdd("h",10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10)+1,[StartDate])) AS Duration
FROM 
    MsysObjects AS Uno, 
    MsysObjects AS Deca, 
    Durations
WHERE 
    DateAdd("h",10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10),[StartDate])<=[EndDate]
GROUP BY 
    DateValue(DateAdd("h",10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10),[StartDate])), 
    Hour(DateAdd("h",10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10),[StartDate])), 
    DateDiff("s",
        DateAdd("h",10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10),[StartDate]),
        DateAdd("h",10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10)+1,[StartDate]));

尊重分钟和秒数:

SELECT 
    DateValue(DateAdd("h",10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10),[StartDate])) AS [Date], 
    Hour(DateAdd("h",10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10),[StartDate])) AS [Hour], 
    DateDiff("s",
        IIf(DateDiff("h",[StartDate],DateAdd("h",10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10),[StartDate]))=0,
            [StartDate],
            DateValue([StartDate])+TimeSerial(Hour([StartDate])+10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10),0,0)),
        IIf(DateDiff("h",[EndDate],DateAdd("h",10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10),[StartDate]))=0,
            [EndDate],
            DateValue([StartDate])+TimeSerial(Hour([StartDate])+10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10)+1,0,0))) AS Duration
FROM 
    MsysObjects AS Uno, 
    MsysObjects AS Deca, 
    Durations
WHERE 
    DateAdd("h",10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10),[StartDate])<[EndDate]
GROUP BY 
    DateValue(DateAdd("h",10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10),[StartDate])), 
    Hour(DateAdd("h",10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10),[StartDate])), 
    DateDiff("s",
        IIf(DateDiff("h",[StartDate],DateAdd("h",10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10),[StartDate]))=0,
            [StartDate],
            DateValue([StartDate])+TimeSerial(Hour([StartDate])+10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10),0,0)),
        IIf(DateDiff("h",[EndDate],DateAdd("h",10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10),[StartDate]))=0,
            [EndDate],
            DateValue([StartDate])+TimeSerial(Hour([StartDate])+10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10)+1,0,0)));

答案 1 :(得分:0)

我设法添加了我想要的表格并且正在运行。但是,当我问第一个问题时,我可能不太清楚。

我想要的是按小时计算的持续时间。您给我的查询将始终返回3600作为持续时间(无论开始日期和结束日期是什么时间)。

这是我在表格中的1条记录的真实示例:

dealerimage

有了这条记录,我想知道的是:

campaign    agent   start_time              end_time
279          542    23/05/2016  17:05:21    24/05/2016 09:58:54

请注意,在第一行和最后一行中,我有start_time和下一小时之间的差异。