计算轮班之间的小时数

时间:2017-12-01 21:57:12

标签: sql-server tsql sql-server-2012

在我们经营的一个城市中,有一项新法律生效,在这些城市中,我们必须至少有11个小时的时间从有人退出,直到他们可以进入。如果时间少于11小时,我们必须为每次事件支付一定金额。

我正在使用SQL Server 2012

我需要帮助确定每个人在轮班之间有多少小时。根据以下记录,我们将欠11/14 11:28 pm - > 11/15 8:56 am(9.28 hrs)和11/29 2:32 am - > 11/29 10:11 am(7.39 hrs)

CREATE TABLE #Shift(
    FKEmployeeNumber int,
    DateOfBusiness datetime,
    FKStoreId int,
    EmployeeShiftNumber int,
    FKJobCodeId int,
    InHour int,
    InMinute int,
    OutHour int,
    OutMinute int)

insert into #Shift ( FKEmployeeNumber, DateOfBusiness, FKStoreId, EmployeeShiftNumber, FKJobCodeId, InHour, InMinute,OutHour,OutMinute)
values
(529251, '11/13/2017', 3013, 0, 1, 8, 1, 16, 24),
(529251, '11/14/2017', 3013, 0, 1, 15, 21, 23, 28),
(529251, '11/15/2017', 3013, 0, 1, 8, 56, 15, 58),
(529251, '11/16/2017', 3013, 0, 1, 14, 59, 20, 54),
(529251, '11/19/2017', 3013, 0, 1, 12, 40, 19, 42),
(529251, '11/20/2017', 3013, 1, 1, 8, 28, 23, 47),
(529251, '11/21/2017', 3013, 0, 1, 15, 31, 23, 30),
(529251, '11/25/2017', 3013, 0, 1, 10, 26, 18, 13),
(529251, '11/27/2017', 3013, 0, 1, 9, 58, 18, 27),
(529251, '11/28/2017', 3013, 0, 1, 14, 59, 2, 32),
(529251, '11/29/2017', 3013, 0, 1, 10, 11, 17, 13),
(529251, '11/29/2017', 3013, 1, 1, 17, 16, 17, 25),
(529251, '11/30/2017', 3013, 0, 1, 15,4, 20, 0),
(529251, '11/30/2017', 3013, 1, 1, 20, 0, 23, 18),
(529251, '11/30/2017', 3013, 2, 1, 23, 18, 23, 22),
(529251, '12/01/2017', 3013, 0, 1, 12, 12, 16, 0)


Select 
sh.dateofbusiness,sh.fkstoreid,sh.employeeshiftnumber,sh.fkemployeenumber, sh.fkjobcodeid,sh.inHour,sh.InMinute,sh.OutHour,sh.OutMinute
 from #shift sh where sh.dateofbusiness > '11/12/2017' and sh.fkemployeenumber = 529251
order by sh.FKEmployeeNumber, sh.DateOfBusiness

1 个答案:

答案 0 :(得分:0)

这是创建一些日期时间之后的一种方式......使用cte和self join。

if object_id('tempdb..#Shift') is not null
drop table #Shift

CREATE TABLE #Shift(
    FKEmployeeNumber int,
    DateOfBusiness datetime,
    FKStoreId int,
    EmployeeShiftNumber int,
    FKJobCodeId int,
    InHour int,
    InMinute int,
    OutHour int,
    OutMinute int)

insert into #Shift ( FKEmployeeNumber, DateOfBusiness, FKStoreId, EmployeeShiftNumber, FKJobCodeId, InHour, InMinute,OutHour,OutMinute)
values
(529251, '11/13/2017', 3013, 0, 1, 8, 1, 16, 24),
(529251, '11/14/2017', 3013, 0, 1, 15, 21, 23, 28),
(529251, '11/15/2017', 3013, 0, 1, 8, 56, 15, 58),
(529251, '11/16/2017', 3013, 0, 1, 14, 59, 20, 54),
(529251, '11/19/2017', 3013, 0, 1, 12, 40, 19, 42),
(529251, '11/20/2017', 3013, 1, 1, 8, 28, 23, 47),
(529251, '11/21/2017', 3013, 0, 1, 15, 31, 23, 30),
(529251, '11/25/2017', 3013, 0, 1, 10, 26, 18, 13),
(529251, '11/27/2017', 3013, 0, 1, 9, 58, 18, 27),
(529251, '11/28/2017', 3013, 0, 1, 14, 59, 2, 32),
(529251, '11/29/2017', 3013, 0, 1, 10, 11, 17, 13),
(529251, '11/29/2017', 3013, 1, 1, 17, 16, 17, 25),
(529251, '11/30/2017', 3013, 0, 1, 15,4, 20, 0),
(529251, '11/30/2017', 3013, 1, 1, 20, 0, 23, 18),
(529251, '11/30/2017', 3013, 2, 1, 23, 18, 23, 22),
(529251, '12/01/2017', 3013, 0, 1, 12, 12, 16, 0)


;with cte as(
select 
    FKEmployeeNumber
    ,DateOfBusiness
    ,FKStoreId
    ,EmployeeShiftNumber
    ,FKJobCodeId
    ,clockin = cast(convert(varchar(10),DateOfBusiness,101) + ' ' + right('00' + cast(InHour as varchar),2) + ':' + right('00' + cast(InMinute as varchar),2) as datetime)
    ,clockout = cast(convert(varchar(10),DateOfBusiness,101) + ' ' +  right('00' + cast(OutHour as varchar),2) + ':' + right('00' + cast(OutMinute as varchar),2) as datetime)
from
    #Shift),

cte2 as(
select *, RN = row_number() over (partition by FKEmployeeNumber order by clockin)
from cte)

select
    c.FKEmployeeNumber
    ,c.DateOfBusiness
    ,c.FKStoreId
    ,c.EmployeeShiftNumber
    ,c.FKJobCodeId
    ,c.clockin
    ,c.clockout
    ,nexClockInHourDifference = datediff(minute,c.clockout,c2.clockin) / 60.0
from cte2 c
left join cte2 c2 on c2.FKEmployeeNumber = c.FKEmployeeNumber and c2.RN = c.RN + 1

drop table #Shift

此外,由于你是2012年,你可以在这里使用LEAD并删除row_number()功能....

;with cte as(
select 
    FKEmployeeNumber
    ,DateOfBusiness
    ,FKStoreId
    ,EmployeeShiftNumber
    ,FKJobCodeId
    ,clockin = cast(convert(varchar(10),DateOfBusiness,101) + ' ' + right('00' + cast(InHour as varchar),2) + ':' + right('00' + cast(InMinute as varchar),2) as datetime)
    ,clockout = cast(convert(varchar(10),DateOfBusiness,101) + ' ' +  right('00' + cast(OutHour as varchar),2) + ':' + right('00' + cast(OutMinute as varchar),2) as datetime)
from
    #Shift)


select
    c.FKEmployeeNumber
    ,c.DateOfBusiness
    ,c.FKStoreId
    ,c.EmployeeShiftNumber
    ,c.FKJobCodeId
    ,c.clockin
    ,c.clockout
    ,nexClockInHourDifference = datediff(minute,c.clockout,lead(c.clockin) over(partition by FKEmployeeNumber order by clockin)) / 60.0
from cte c

drop table #Shift