计算工作时间在2个日期之间
我有一个预订桌,我需要计算工作人员一周内工作的总小时,但是我需要在午夜至午夜的下一个星期一之间切断日期。
create table Bookings (ID int IDENTITY(1,1) not null, start datetime, finish datetime, staffId int)
insert into Bookings (start, finish, staffId) values ('2022-06-19 21:00:00', '2022-06-20 07:00:00', 1)
insert into Bookings (start, finish, staffId) values ('2022-06-24 21:00:00', '2022-06-25 07:00:00', 1)
insert into Bookings (start, finish, staffId) values ('2022-06-25 21:00:00', '2022-06-26 07:00:00', 1)
insert into Bookings (start, finish, staffId) values ('2022-06-26 21:00:00', '2022-06-27 07:00:00', 1)
select *, datediff(MINUTE, start, finish)/60.0
from Bookings
where staffid = 1 and start between '2022-06-19' and '2022-06-27'
我需要第1行才能在2022-06-06-20 00:00开始,第4行才能在2022年完成-06-27 00:00因此,第1行的小时为7,而第4行的时间为3,因此总计30小时而不是40小时。
关于如何执行此操作的任何想法?
I have a Bookings table and I need to calculate the total hours a staff member has worked in a week, but I need it to cut off dates between Monday midnight and the following Monday at midnight.
create table Bookings (ID int IDENTITY(1,1) not null, start datetime, finish datetime, staffId int)
insert into Bookings (start, finish, staffId) values ('2022-06-19 21:00:00', '2022-06-20 07:00:00', 1)
insert into Bookings (start, finish, staffId) values ('2022-06-24 21:00:00', '2022-06-25 07:00:00', 1)
insert into Bookings (start, finish, staffId) values ('2022-06-25 21:00:00', '2022-06-26 07:00:00', 1)
insert into Bookings (start, finish, staffId) values ('2022-06-26 21:00:00', '2022-06-27 07:00:00', 1)
select *, datediff(MINUTE, start, finish)/60.0
from Bookings
where staffid = 1 and start between '2022-06-19' and '2022-06-27'
I need row 1 to start at 2022-06-20 00:00 and row 4 to finish at 2022-06-27 00:00 so the hours for row 1 would be 7, and for row 4 would be 3, thus totaling 30 hours instead of 40.
Any ideas on how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑您需要在这里做什么,首先要更改在其中查看
完成
时间 >启动时间 您的完成值。然后,在工作时间内,您需要使用case
表达式返回列或输入参数值,这取决于较小/更大:I suspect what you need to do here is firstly change your
WHERE
to look at thefinish
time being after your start value, and thestart
time being before your finish value. Then for the hours worked, you need to use aCASE
expression to return the column or input parameters value, depending which is lesser/greater: