Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 营业时间逻辑和营业时间例外情况_Sql_Sql Server - Fatal编程技术网

Sql 营业时间逻辑和营业时间例外情况

Sql 营业时间逻辑和营业时间例外情况,sql,sql-server,Sql,Sql Server,我已使用以下工具创建了日期维度: 这只是一个标准的,我为OpeningTime和ClosingTime添加了两列,其值分别为08:00:00和18:00:00 我还有一个计算字段,作为非持久性计算字段,它计算出开始时间和结束时间之间的分钟差 我有下面的逻辑,但在给你这个之前,让我设置一个场景,展示业务情况的背景,因此它们通常在周一至周五上午8点至下午6点开放,也在上午8点至下午1点开放 不过,这些规定也有例外,因为它们可以开放更长时间,甚至在周日。由于某些原因,我下面的代码在星期天显示较少,因

我已使用以下工具创建了日期维度:

这只是一个标准的,我为OpeningTime和ClosingTime添加了两列,其值分别为08:00:00和18:00:00

我还有一个计算字段,作为非持久性计算字段,它计算出开始时间和结束时间之间的分钟差

我有下面的逻辑,但在给你这个之前,让我设置一个场景,展示业务情况的背景,因此它们通常在周一至周五上午8点至下午6点开放,也在上午8点至下午1点开放

不过,这些规定也有例外,因为它们可以开放更长时间,甚至在周日。由于某些原因,我下面的代码在星期天显示较少,因为很少有情况下,如果他们在星期天上午8点到下午1点开放。显然,即使是在星期天,无论延长开放时间是什么,我都必须手动将其添加到Dim_Date日历中,对于starttime和enddate,我都有00:00:00,默认情况下,除非业务部门告诉我,否则datediff的最小值为0

但是下面的代码没有考虑到这一点,并且计算量比它应该的要少,如果有人能提供解决方案,代码有问题,基本上我只希望代码灵活,所以我只对日历进行更改,即使是延长的工作时间或不正常的工作时间,并且代码反映了这一点。多谢各位

declare @Date1 datetime = '2017-08-01 08:00:00'
declare @Date2 datetime = '2017-08-07 09:10:00'


declare @StartTime time = cast(@Date1 as time)
declare @EndTime time = cast(@Date2 as time)
declare @CCStartTime time = (select StartTime from dim_date where id = convert(nvarchar(8),@Date1,112))
declare @CCEndTime time = (select EndTime from dim_date where id = convert(nvarchar(8),@Date2,112))


declare @ActualStart time = (select case 
            when datename(DW,@Date1)='Sunday' then '00:00:00'
            when @StartTime between '00:00:00' and @CCStartTime then @CCStartTime
            when @StartTime between  @CCStartTime and @CCEndTime then @StartTime
            when @StartTime between @CCEndTime and  '23:59:59' then @CCEndTime end)

declare @ActualEnd time = (select case 
            when datename(DW,@Date2)='Sunday' then '00:00:00'
            when @EndTime between '00:00:00' and @CCStartTime then @CCStartTime
            when @EndTime between @CCStartTime and @CCEndTime then @EndTime
            when @EndTime between @CCEndTime and '23:59:59' then @CCEndTime end)


declare @DiffrenceStart int = isnull(DATEDIFF(minute,@CCStartTime,@ActualStart),0)

declare @DiffrenceEnd int = isnull(DATEDIFF(minute,@ActualEnd,@CCEndTime),0)

/*
select @StartTime as StartDate
select @EndTime as EndDate
select @CCStartTime as CCStartDate
select @CCEndTime as CCEndDate
select @ActualStart as ActualStart
select @ActualEnd as ActualEnd
select abs(@DiffrenceStart) as DiffrenceStart 
select abs(@DiffrenceEnd) as DifrenceEnd
*/



select sum(Min)- (@DiffrenceStart + @DiffrenceEnd)
from dim_date
where id between convert(nvarchar(8),@Date1,112) and convert(nvarchar(8),@Date2,112)
看看这个:

declare @ActualEnd time = (select case 
            when datename(DW,@Date2)='Sunday' then '00:00:00'
难道不是:

declare @ActualEnd time = (select case 
            when datename(DW,@Date2)='Sunday' then '23:59:59'

这么多的信。。。很难区分到底是什么问题,你能简单地问一下你的问题吗?只需将描述、问题和预期结果分开即可。您可以投影您正在寻找的样本数据和结果吗?这使我们能够更好地理解。表达式
convert(nvarchar(8),@date1112)
将返回格式为“yyyymmdd”的日期字符串。您正在将该值分配给
time
变量,因此结果将始终为'00:00:00.0000000'。我不确定是否遵循您的逻辑,请添加数据样本和预期结果