我有两张桌子,我们称之为timeclock
和time_tracker
。
两个表都包含timestamps
和时间戳。如果一个worker实际上在某个特定的应用程序中,time_tracker只会创建一个“time_in”条目,然后在他们将其工作标记为完成时创建一个“time_out”条目。
在某一天的time_tracker中会有几个这样的条目,而在时钟中只有一个和一个。
我正在尝试确定工作时间与计时时间之间的关系,并以百分比形式显示。
到目前为止,我所做的工作一直有效,直到我加入时钟表。当我这样做时,似乎时间会以某种方式成倍增加,而我无法确定故障的位置。
以下查询:
SELECT it.user_id, SUM(TIMESTAMPDIFF(minute, it.time_in, it.time_out) / 60) as `Total Time Clocked In`,
SUM(TIMESTAMPDIFF(minute, isb.start_time, isb.end_time) / 60) as `Total Time in Work`
FROM time.time_track isb
INNER JOIN time.timeclock it ON isb.user_id = it.user_id
WHERE it.time_in >= '2013-10-01 00:00:00' AND it.time_out <= '2013-10-01 23:59:59'
AND isb.start_time >= '2013-10-01 00:00:00' AND isb.end_time <= '2013-10-01 23:59:59'
AND it.user_id = '12';
答案 0 :(得分:0)
如果问题是“您的总时间”太高,这应该可以解决问题。从该表达式中删除SUM
函数。
SELECT it.user_id, TIMESTAMPDIFF(minute, it.time_in, it.time_out) / 60 as `Total Time Clocked In`,
SUM(TIMESTAMPDIFF(minute, isb.start_time, isb.end_time) / 60) as `Total Time in Work`
FROM time.time_track isb
INNER JOIN time.timeclock it ON isb.user_id = it.user_id
WHERE it.time_in >= '2013-10-01 00:00:00' AND it.time_out <= '2013-10-01 23:59:59'
AND isb.start_time >= '2013-10-01 00:00:00' AND isb.end_time <= '2013-10-01 23:59:59'
AND it.user_id = '12';
编辑 - 我没有意识到特定日期的时钟表中可能有多条记录。以下陈述应该适用于该场景。
SELECT a.user_id, a.`Total Time Clocked In`, b.`Total Time in Work`
FROM
(
SELECT it.user_id, SUM(TIMESTAMPDIFF(minute, it.time_in, it.time_out)) / 60 as `Total Time Clocked In`
FROM time.timeclock it
WHERE it.time_in >= '2013-10-01 00:00:00' AND it.time_out <= '2013-10-01 23:59:59'
AND it.user_id = '12'
) AS a
JOIN
(
SELECT isb.user_id, SUM(TIMESTAMPDIFF(minute, isb.start_time, isb.end_time) / 60) as `Total Time in Work`
FROM time.time_track isb
WHERE isb.start_time >= '2013-10-01 00:00:00' AND isb.end_time <= '2013-10-01 23:59:59'
AND isb.user_id = '12'
) AS b ON a.user_id = b.user_id