如何使用开始和结束时间列制作时间存储库?
我有3列,员工_id,start_time和end_time,我想赚1小时的钱,向我展示每小时有多少员工工作。例如,员工A从下午12点到下午3点工作,雇员B从下午2点到下午4点工作,因此,下午12点(1名员工在工作)下午1点(1名员工)下午2点(2名员工在工作)3 pm( 2个员工)和下午4点(1名员工),我该如何在SQL中进行?让我向您展示开始和结束时间列的图片。
样本输入将为:
预期的结果将是
我想创建一个水桶,以了解一天中的每个小时都在工作。
选择
Employee_id,
TIME(shift_start_at,timezone) AS shift_start,
TIME(shift_end_at,timezone) AS shift_end,
FROM
`employee_shifts` AS shifts
WHERE
DATE(shifts.shift_start_at_local) >= "2022-05-01"
GROUP BY
1,
2,
3
I have 3 columns, employee_id, start_time and end_time I want to make bucks of 1 hour to show me how many employees were working in each hour. For example, employee A worked from 12 pm to 3 pm and employee B worked from 2 pm to 4 pm so, at 12 pm (1 employee was working) 1 pm (1 employee) 2 pm (2 employees were working) 3 pm (2 employees) and 4 pm (1 employee), how can I make this in SQL? Let me show you a picture of the start and end time columns.
Sample input would be:
Expected outcome would be something like
I want to create a bucket in order to know how many people were working in each hour of the day.
SELECT
Employee_id,
TIME(shift_start_at,timezone) AS shift_start,
TIME(shift_end_at,timezone) AS shift_end,
FROM
`employee_shifts` AS shifts
WHERE
DATE(shifts.shift_start_at_local) >= "2022-05-01"
GROUP BY
1,
2,
3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设您在MySQL版本8或更高版本上生成了所有存储桶,请加入开始在启动时间范围内移动到填充时间,请过滤出那些不适用的时间,然后计算例如: -
Assuming you are on mysql version 8 or above generate all the buckets , left join to shifts to infill times in start-endtime ranges , filter out those that are not applicable then count eg:-
如果您的时间桶数量有限,也许您可
以以这种方式使用它,我应该强调,如果您有有限的存储桶。如果您有很多存储桶,则可以使用存储桶创建一个基础表,然后
左JOIN
它以获取结果。If you have a limited number of time bucket maybe you can use it this way
Althought, I should emphasize that this solution works if you have a limited bucket. If you have a lot of buckets, you can create a base table with your buckets then
LEFT JOIN
it to get your results.只需在每列中使用一个子查询,即提及两者之间所需的时间戳,还请确保您的start_time和end_time列是时间戳类型。有关更多信息,请共享表结构,示例数据和预期输出
Just use a subquery for each column mentioning the required timestamp in between, also make sure your start_time and end_time columns are timestamp types. For more information, please share the table structure, sample data, and expected output
如果我很好地了解,这将是
日程安排是一张具有员工时间表的表。
If I understood well, this would be
Where schedule is a table with employee schedules.