简体   繁体   English

MS Access \\ Jet SQL-员工上班和下班时间,同一张表,每个员工的总工作时间

[英]MS Access\Jet SQL - staff clock in & out times, same table, sum full time worked per staff

I have the below table named 'timings'. 我有一个名为“ timings”的下表。 It holds the comings and goings of staff members (morn_in, morn_out, afternoon_in, afternoon_out). 它保存着工作人员的来往情况(早上进,早上出,下午进,下午出)。

timings table What I would like to achieve is an output grouped by staff_ref, SUM of their hours worked, like so; 计时表我想要实现的是一个按staff_ref分组的输出,其工作时间的总和是这样的;

staff_ref|   total_worked|   
   U1            18:00

I currently use 4 separate queries to select each of the 'type' (morn_in, morn_out etc), the one for type='morn_in' is below, this query is called [101 am in] (it gets referenced later); 我目前使用4个单独的查询来选择每个“类型”(morn_in,morn_out等),下面是用于type ='morn_in'的查询,该查询称为[101 am in](稍后会引用);

SELECT 
staff_ref
, time
, Format([date],"dd/mm/yyyy") AS t_date
FROM timings
WHERE (((type)='morn_in'));  

I then SUM the DateDiff between the morning times; 然后,我将早晨时间之间的DateDiff求和;

SELECT 
[101 am in].staff_ref
, Sum(DateDiff("n",[101 am in].[time],[102 am out].[time])) AS morning_mins

FROM [101 am in] INNER JOIN [102 am out] 
 ON ([101 am in].staff_ref = [102 am out].staff_ref) 
 AND ([101 am in].date = [102 am out].date)
GROUP BY [101 am in].staff_ref;

And then SUM the datediff on the afternoon times (the same as above) separately in another query, then I add the result of those two queries together to get my total. 然后在另一个查询中分别求和下午时间的datediff(与上面相同),然后将这两个查询的结果相加在一起得出总数。

As you can see its very convoluted, and its not simple to explain. 如您所见,它非常复杂,而且解释起来并不简单。

I would like to know how I can combine the queries so I only need one statement to return the results if possible. 我想知道如何组合查询,因此如果可能的话,我只需要一个语句即可返回结果。

If I've explained it terribly then let me know and I'll make amendments. 如果我已经做了详尽的解释,请告诉我,我将进行修正。
Thanks 谢谢

Edit 1 - 03/07/2018 编辑1-03/07/2018
I have used the code below to pull the data I need, since its a 'time' data type I had to calculate the total minutes then perform \\60 for the hours and MOD 60 for the minutes as the results were over 24 hours. 我使用下面的代码提取所需的数据,因为它是一种“时间”数据类型,因此我必须计算总分钟数,然后对小时进行\\ 60,对于分钟进行MOD 60,因为结果超过24小时。

SELECT 
staff_ref, 
CLng(24*60*CDate(Sum(IIf([type]='morn_out',[time],0)-IIf([type]='morn_in',[time],0)+
IIf([type]='afternoon_out',[time],0)-IIf([type]='afternoon_in',[time],0)))) AS time_mins,
[time_mins]\60 & Format([time_mins] Mod 60,"\:00") AS convert_backHHMM

FROM timings

GROUP BY 
staff_ref, 
fix(time)
;

I would like to know how I would handle the possibility of the table having a "morning_out" time but NO "morning_in" time (or vice versa). 我想知道如何处理表具有“ morning_out”时间而没有“ morning_in”时间(反之亦然)的可能性。 [The front end of the system does protect against this, but I'm really curious and want to learn}. [系统的前端确实可以防止这种情况,但是我很好奇,想学习}。

Thanks for any assistance 感谢您的协助

Try this: 尝试这个:

SELECT 
    staff_ref,
    Sum(IIf([Type] = "morn_out", [date], 0) - IIf([Type] = "morn_in", [date], 0) +
        IIf([Type] = "afternoon_out", [date], 0) - IIf([Type] = "afternoon_in", [date], 0)) As TotalTime
FROM 
    [101 am in]
GROUP BY
    staff_ref,
    Fix([date]);

Apply a format to TotalTime of, say, h:nn 将格式应用于TotalTime,例如h:nn

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM