[英]SQL Query calculates total time worked by user
I am trying to create a SQL query that can calculate total time Grouped BY USER. 我正在尝试创建一个SQL查询,该查询可以计算按用户分组的总时间。
For example: if John Smith does one job for 30 minutes and a different job for 45 minutes creating two records in my database. 例如:如果约翰·史密斯(John Smith)做一份工作30分钟,而另一份工作进行45分钟,则在数据库中创建了两条记录。 I need a result that would display
我需要显示的结果
USER Time Worked Total Date
John Smith 75 XX/XX/XXXX
What I have is this. 我有这个。
USER Time Worked Total Date
John Smith 45 XX/XX/XXXX
John Smith 30 XX/XX/XXXX
I can calculate the number of minutes per job done but not the total Query Code: 我可以计算每个工作完成的分钟数,但不能计算查询总数:
SELECT DISTINCT
Shipping_Table.Employee,
Shipping_Table.Date,
DateDiff("n",Shipping_Table.Start_Time,Shipping_Table.End_Time) AS MinutesWorked
FROM Shipping_Table, Employees
WHERE Shipping_Table.Date=[Forms]![Time Query]![DateCalc]
I am using a form so the USER can just pick a date and check to see how many hours his employees are working. 我使用的是表格,因此USER可以只选择一个日期并查看其员工工作了几个小时。 The way the database table is setup the time entries are a Date/Time variable.
数据库表的设置方式是,时间条目是日期/时间变量。
You're close... just wrap your minutesWorked calculation in a Sum() function and add a group by, and with the group by, you don't need the distinct. 您快结束了……只需将minutesWorked计算结果包装在Sum()函数中,然后添加一个group by,并且与group by一起,您就不需要非重复。
SELECT Shipping_Table.Employee, Shipping_Table.Date,
suM(DateDiff("n",Shipping_Table.Start_Time,Shipping_Table.End_Time)) AS MinutesWorked
FROM Shipping_Table, Employees
WHERE Shipping_Table.Date=[Forms]![Time Query]![DateCalc]
GROUP BY Shipping_Table.Employee, Shipping_Table.Date
Note: your join is missing how shipping_table and employees relate which results in a Cartesian product which can lead to unexpected results. 注意:您的联接缺少shipping_table和雇员之间的关系,从而导致笛卡尔积,这可能导致意外结果。
you should really define how the tables relate to eachother (likely on employeeID and/or date) 您应该真正定义表格之间的关系(可能在employeeID和/或日期上)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.