日期之间的SQL,但在某个时间切断并继续和某个时间

时间:2010-08-05 14:43:51

标签: sql sql-server sql-server-2005

我有一个MSSQL查询,可以在一段时间内运行订单生产力。基本上它会计算订单从开始日期到完成日期的分钟数。

我正在做的是获取创建订单的日期和时间戳(a)并获取订单完成的日期和时间戳(b)并减去它们然后乘以1440以得到之间的分钟数2个日期。

,FLOOR(((CAST(b.[AuditInsertTimestamp2] AS FLOAT)) - (CAST(a.[AuditInsertTimestamp2] AS FLOAT))) * 1440) AS [Minutes]

这完全正常,但这也算是我们业务关闭的时间。我想做的就是回到我们的业务开放时(上午6:30 - 下午5:00)这些日期之间的时间。日期可以跨越多天或仅一天。有什么想法?!?!?!

感谢!!!!

3 个答案:

答案 0 :(得分:1)

我会使用您的代码来计算这两个日期之间的所有分钟,但之后我会减去您的业务关闭的分钟数。

为此,请使用this function计算工作天数(从而删除周末)并将输出乘以630(10小时半* 60 =您的业务每天的分钟数)打开)。

全部添加,就像这样:

(your result) - (number of work days between the beginning and the end of the request) * 630

虽然不会考虑假期。 干杯!

编辑:由于该链接需要注册,因此这里是代码本身的简短版本;检查原始来源的详细信息,因为我不是它的作者:

SELECT
   (DATEDIFF(dd, @StartDate, @EndDate) + 1)
  -(DATEDIFF(wk, @StartDate, @EndDate) * 2)
  -(CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END)
  -(CASE WHEN DATENAME(dw, @EndDate) = 'Saturday' THEN 1 ELSE 0 END)

答案 1 :(得分:1)

如果有人想知道,这是最后的询问......

/****** Object:  UserDefinedFunction [dbo].[udf_TEST]    Script Date: 08/11/2010 16:49:06 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE FUNCTION [dbo].[udf_TEST]  
    (  
        @StartDate DATETIME,  
        @EndDate DATETIME  
    )  
    RETURNS VARCHAR(MAX)  

    --DECLARE @StartDate DATETIME  
    --DECLARE @EndDate DATETIME  
    --SET @StartDate = '2010-07-06 14:46:37.577'   
    --SET @EndDate = '2010-07-09 09:04:31.290'  
    BEGIN  
    DECLARE @FinalMinutes AS DECIMAL  
    IF (CONVERT(VARCHAR(13), @StartDate, 114) < CONVERT(VARCHAR(13), @EndDate, 114))  
    BEGIN  
    DECLARE @NonWorkTime1 INT  
    SET @NonWorkTime1 = 780  
    --How many minutes are between order start and end time including non working time  
    DECLARE @AllMins1 INT   
    --Declares how many minutes are in a day and makes it float to get remainder minutes when divided  
    DECLARE @MinsInDay1 DECIMAL  
    SET @MinsInDay1 = 1440.0  
    --Finds how many minutes are between start and end time excluding weekends and assignes to variable   
    SET @AllMins1 = ((DATEDIFF(mi, @StartDate, @EndDate))   
      -(((DATEDIFF(wk, @StartDate, @EndDate) * 2) * 24) * 60)    
      -(((CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END) * 24) * 60)  
      -(((CASE WHEN DATENAME(dw, @EndDate) = 'Saturday' THEN 1 ELSE 0 END) * 24) * 60))   
    --Calculates how many days have elapsed in the minutes that the order has taken  
    DECLARE @MinDays1 INT  
    SET @MinDays1 = (@AllMins1/@MinsInDay1)  
    --Subtracts complete day non worked minutes from final minutes between orders  
    SET @FinalMinutes = (@AllMins1 - (@MinDays1 * @NonWorkTime1) + 360 + 420)  
    END  
    ELSE  
    BEGIN   
    --How many minutes a day are not worked for trips  
    DECLARE @NonWorkTime INT  
    SET @NonWorkTime = 780  
    --How many minutes are between order start and end time including non working time  
    DECLARE @AllMins INT   
    --Declares how many minutes are in a day and makes it float to get remainder minutes when divided  
    DECLARE @MinsInDay DECIMAL  
    SET @MinsInDay = 1440.0  
    --Finds how many minutes are between start and end time excluding weekends and assignes to variable   
    SET @AllMins = ((DATEDIFF(mi, @StartDate, @EndDate))   
      -(((DATEDIFF(wk, @StartDate, @EndDate) * 2) * 24) * 60)    
      -(((CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END) * 24) * 60)  
      -(((CASE WHEN DATENAME(dw, @EndDate) = 'Saturday' THEN 1 ELSE 0 END) * 24) * 60))   
    --Calculates how many days have elapsed in the minutes that the order has taken  
    DECLARE @MinDays INT  
    SET @MinDays = (@AllMins/@MinsInDay)  
    --Subtracts complete day non worked minutes from final minutes between orders  
    SET @FinalMinutes = (@AllMins - (@MinDays * @NonWorkTime))  
    END 
    RETURN -(@FinalMinutes) 
    END  

在我的主查询中,我有一个案例陈述,如果它在@StartDate和@Enddate之间不到一天,它只是减去时间,否则如果它超过一天或不到一天但是不到24小时它把它发送到这个功能。一切似乎都很完美!

答案 2 :(得分:0)

最简单的方法是,计算天数并乘以630分钟(每天营业时间为10.5小时)。

如果您的解决方案需要比这更复杂(不包括周末,假期等),我会说逻辑更好地脱离了数据库和业务层。