首先,我想说我是SSMS的新手,所以如果有些事情还不太清楚,请多多包涵。所以这是我的问题。
我正在尝试计算工作时间内2个日期的时间,并且应该不包括周末和节假日。
我找到了一个功能脚本,可以计算工作时间内的时间,并且确实不包括周末。现在我只是错过了排除假期的部分。我做了一个假期表,其中包含假期的名称和日期。
我发现了另一个函数,该函数可以计算没有假期的时间,但是当我用它运行查询时,我得到的输出太多了。而且我不确定出了什么问题。
这是我正在使用的新功能,它让太多的输出。
CREATE FUNCTION getWorkingHoursDiff(
@startDate datetime,
@endDate datetime)
RETURNS int AS
BEGIN
DECLARE @totaldays INT;
DECLARE @weekenddays INT;
DECLARE @ShiftStartOn datetime, @ShiftEndOn datetime
Declare @startHourDiff int, @endHourDiff int
set @ShiftStartOn = cast(CONVERT(VARCHAR,@startDate,110)+' 08:00' as Datetime)
set @ShiftEndOn = cast(CONVERT(VARCHAR, @endDate,110)+' 18:00' as Datetime)
declare @ShiftHours int
set @ShiftHours = DATEDIFF(HOUR, @ShiftStartOn, @ShiftEndOn)
set @startHourDiff = 0
if @startDate between @ShiftStartOn and DATEADD(hour, @ShiftHours, @ShiftStartOn)
begin
set @startHourDiff = DATEDIFF(HOUR, @ShiftStartOn, @startDate)
set @startDate = @ShiftStartOn
if(@startHourDiff < 0)
begin
set @startHourDiff = 0
end
end
if DATEPART(WEEKDAY, @startDate) = 1 or DATEPART(WEEKDAY, @startDate) = 7
set @startHourDiff = 0
if exists(select 1 from table_holiday where DATEDIFF(day, start_time, @startDate) = 0)
set @startHourDiff = 0
if @startDate > DATEADD(hour, @ShiftHours, @ShiftStartOn)
set @startDate = DATEADD(day,1,@ShiftStartOn)
set @endHourDiff = 0
if @endDate between DATEADD(hour, -@ShiftHours, @ShiftEndOn) and @ShiftEndOn
begin
set @endHourDiff = DATEDIFF(HOUR, @endDate, @ShiftEndOn)
set @endDate = @ShiftEndOn
if(@endHourDiff < 0)
set @endHourDiff = 0
end
if DATEPART(WEEKDAY, @endDate) = 1 or DATEPART(WEEKDAY, @endDate) = 7
set @endHourDiff = 0
if exists(select 1 from table_holiday where DATEDIFF(day, start_time, @endDate) = 0)
set @endHourDiff = 0
if @endDate < DATEADD(hour, -@ShiftHours, @ShiftEndOn)
set @endDate = DATEADD(day,-1,@ShiftEndOn)
SET @totaldays = DATEDIFF(DAY, @startDate, @endDate) +1
SET @weekenddays = ((DATEDIFF(WEEK, @startDate, @endDate) * 2) + -- get the number of weekend days in between
CASE WHEN DATEPART(WEEKDAY, @startDate) = 1 THEN 1 ELSE 0 END + -- if selection was Sunday, won't add to weekends
CASE WHEN DATEPART(WEEKDAY, @endDate) = 7 THEN 1 ELSE 0 END) -- if selection was Saturday, won't add to weekends
select @totaldays = @totaldays - @weekenddays
declare @chkdtFromDate datetime, @chkdtToDate datetime
select @chkdtFromDate = DATEADD(dd, DATEDIFF(dd, 0, @startDate), 0)
select @chkdtToDate = DATEADD(dd, DATEDIFF(dd, 0, @endDate), 0)
select @totaldays = @totaldays - COUNT(1) from table_holiday where (start_time between @chkdtFromDate and @chkdtToDate)
return (@totaldays*@ShiftHours) - @startHourDiff - @endHourDiff
End
Go
这显示了具有我添加的两个功能的输出。正如您在结果中看到的那样,第一个功能工作得很好。但这并不排除假期。
您看到的第二个结果提供了更多的小时,如OpenstaandeTijdInUren
列所示,而自5月30日至30日是假期,它应该为我提供10个小时的值。
答案 0 :(得分:0)
查找工作日,下面是一个愚蠢的例子
async getAccelerometerData() {
const accelerometer = await accelerometerData();
accelerometer.subscribe(({ x, y, z }) => {
this.setState({ x, y, z }),
error => {
console.log("The sensor is not available");
};
});
}
}