I'm trying to create a query that will accrue the number of employees on shift, the entire 24 hours day. So when a user takes a shift that begin 2013-06-17 10:00:00 AND ends 14:00:00, I'd like the user to "count" in 5 cells
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
This should end up providing me with a 24 hour range, showing me how many employees that are on job in the specific hour.
I have 3 tables that I use.
teamslots
---------
id
startdate
starttime
endtime
teamslot_schedule
-----------------
id
slotid (joins to is in teamslots)
userid
shifthours
----------
thehour
In shifthours I have 24 records - 0 - 23 one for each hour I need.
I then tried to LEFT JOIN on HOUR(teamslots.starttime) AND GROUP BY startdate,thehour but with no real luck.
Here's my query (that currently returns 24 rows of thehour,null,null)
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;
Could anyone please help me out here - point me in the right direction ?