简体   繁体   English

mysql-查询以查找每天每小时产生的总和

[英]mysql - query to find SUM accrued per hour per day

I'm trying to create a query that will accrue the number of employees on shift, the entire 24 hours day. 我正在尝试创建一个查询,该查询将在整个24小时内增加轮班员工的数量。 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: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

This should end up providing me with a 24 hour range, showing me how many employees that are on job in the specific hour. 这应该最终为我提供24小时的工作范围,向我显示在特定小时内有多少员工在工作。

I have 3 tables that I use. 我有3张桌子。

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. 在轮班时间里,我有24条记录-0-23需要的每一小时一条。

I then tried to LEFT JOIN on HOUR(teamslots.starttime) AND GROUP BY startdate,thehour but with no real luck. 然后,我尝试在HOUR(teamslots.starttime)和GROUP BY startdate上左加入,在该小时内没有任何运气。

Here's my query (that currently returns 24 rows of thehour,null,null) 这是我的查询(当前返回小时,空,空的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;

Could anyone please help me out here - point me in the right direction ? 有人可以在这里帮助我-向正确的方向指出我吗?

Brief idea. 简短的想法。 Come up with a range of hours on the relevant day (assuming the day is a known in advance for the query) and do a JOIN where this is between the start and end time. 给出相关日期的某个小时范围(假设查询是提前知道这一天的时间),并进行JOIN,该时间介于开始时间和结束时间之间。

Like this:- 像这样:-

SELECT DATE_ADD('2013/06/17', INTERVAL thehour HOUR), COUNT(teamslot_schedule.id)
FROM shifthours
LEFT OUTER JOIN teamslots
ON DATE_ADD('2013/06/17', INTERVAL thehour HOUR) BETWEEN starttime AND starttime AND endtime
LEFT OUTER JOIN teamslot_schedule
ON teamslots.id = teamslot_schedule.slotid
GROUP BY DATE_ADD('2013/06/17', INTERVAL thehour HOUR)

Note that this is not going to work straight out as I am not sure how your starttime / endtime relate to the startdate. 请注意,由于我不确定您的开始时间/结束时间与开始日期之间的关系,因此无法完全解决此问题。

The following query will include buckets for hours when no one worked: 以下查询将包含无人工作数小时的存储桶:

SELECT
    DATE_FORMAT(d.startdate + INTERVAL s.thehour HOUR, '%Y-%m-%d %H') AS date,
    COUNT(DISTINCT ts.userid) AS users
FROM
    shifthours s
JOIN
    (SELECT DISTINCT startdate FROM teamslots) d
LEFT JOIN
    teamslots t ON t.startdate = d.startdate AND
        s.thehour BETWEEN HOUR(t.starttime) AND HOUR(t.endtime)
LEFT JOIN
    teamslot_schedule ts ON ts.slotid = t.id
GROUP BY
    d.startdate,
    s.thehour
ORDER BY
    d.startdate + INTERVAL s.thehour HOUR;

For a working example see http://sqlfiddle.com/#!2/4a716/13 有关工作示例,请参见http://sqlfiddle.com/#!2/4a716/13

The following query will only include rows when someone worked: 以下查询仅在有人工作时才包含行:

SELECT
    DATE_FORMAT(t.startdate + INTERVAL s.thehour HOUR, '%Y-%m-%d %H') AS date,
    COUNT(DISTINCT ts.userid) AS users
FROM
    shifthours s
INNER JOIN
    teamslots t ON s.thehour BETWEEN HOUR(t.starttime) AND HOUR(t.endtime)
LEFT JOIN
    teamslot_schedule ts ON ts.slotid = t.id
GROUP BY
    t.startdate,
    s.thehour
ORDER BY
    t.startdate + INTERVAL s.thehour HOUR;

For a working example see http://sqlfiddle.com/#!2/4a716/15 有关工作示例,请参见http://sqlfiddle.com/#!2/4a716/15

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

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