【发布时间】:2013-06-17 15:51:37
【问题描述】:
我正在尝试创建一个查询,该查询将累积全天 24 小时轮班的员工人数。因此,当用户从 2013 年 6 月 17 日 10:00:00 开始并结束 14:00:00 轮班时,我希望用户在 5 个单元格中“计数”
2013-06-17 10 : +1 here
2013-06-17 11 : +1 here
2013-06-17 12 : +1 here
2013-06-17 13 : +1 here
2013-06-17 14 : +1 here
这最终应该会为我提供 24 小时的范围,显示在特定时间有多少员工在工作。
我有 3 个我使用的表。
teamslots
---------
id
startdate
starttime
endtime
teamslot_schedule
-----------------
id
slotid (joins to is in teamslots)
userid
shifthours
----------
thehour
在轮班时间,我有 24 条记录 - 0 - 23 条记录,每小时需要一条。
然后我尝试在 HOUR(teamslots.starttime) 和 GROUP BY startdate,thehour 上离开,但没有真正的运气。
这是我的查询(当前返回 24 行的小时,空,空)
SELECT a.thehour,b.startdate,b.taken FROM shifthours a LEFT JOIN (SELECT startdate,starttime,endtime,count(DISTINCT b.id) as taken FROM teamslots a
LEFT JOIN teamslot_schedule b ON a.id=b.slotid) b ON a.thehour=HOUR(b.starttime)
GROUP BY b.startdate,a.thehour;
有人可以帮我吗?指出正确的方向吗?
【问题讨论】:
-
请在SQL Fiddle 上设置您的示例架构和测试数据,以便我们为您提供您可以执行的答案,而无需我们重复设置。
标签: mysql