简体   繁体   English

SQL Server-月的总小时数

[英]SQL Server - Total Hours in month

I have the case when i need to calculate the total contracted hours of an employee in a given 4 week period. 我有一个案例,当我需要计算给定4周内某个员工的总合同工时。 The employee may change hours mid period and hence the total contracted hours would need to adjust based on when the hours changed 员工可能会在中段更改工作时间,因此合同总工时需要根据工作时间的变化进行调整

My SQL is 我的SQL是

declare @start datetime = '2017-06-20'
declare @end datetime = '2017-07-20'

select j.EMPLOY_REF, 
j.ACT_HOURS * 4 as ContractedHoursPer4WeekPeriod, 
j.ACT_HOURS as ContractedHoursPerWeek, 
(j.ACT_HOURS / 7) as ContractedHoursPerDay,
j.COSTCENTRE, 
j.FROMDATE, 
j.UNTILDATE 

from jobholdr j

where EMPLOY_REF ='000033504'

and the result being 结果是

在此处输入图片说明

If the period ran in this example from 20th June 2017 until 20 July 2017 then they should have 10 days at 39 hours per week and then 20 days at 30 hours per week. 如果本例中的时间段是从2017年6月20日到2017年7月20日,那么他们应该每周39小时有10天,然后每周30小时有20天。

How can this be calculated to give one result for total contracted hours in the period given the difference of contracted hours over the period. 给定期间内合同工时数的差异,如何计算得出该期间合同工时数的总和。 I am guessing it needs to be done by breaking it down into days but I am not sure where to go next... 我猜想需要将它分解成几天来完成,但是我不确定下一步要去哪里...

You can include in your SELECT the following 您可以在SELECT中包括以下内容

DATEDIFF(day, CASE WHEN @start > j.fromdate 
                   THEN @start 
                   WHEN @start = j.fromdate 
                   THEN j.fromdate 
                   ELSE DATEADD(day,-1,j.fromdate)
               END, 
              CASE WHEN @end > j.untildate 
                   THEN j.untildate 
                   ELSE @end
               END) * (j.act_hours / 7) TotalContractedHours,

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

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