2

你能帮帮我吗,我正在使用下面的 sql 视图(然后我在水晶报告中使用)。我需要日期差异(以分钟为单位),但现在我需要排除周末。请帮忙 :)

SELECT intwc                             AS wc,
       Datediff(n, start_date, end_date) AS time,
       mh_start_date                     AS date,
       'Repair'                          AS type
FROM   dbo.xxxxxxx 
4

4 回答 4

5

这是@bendataclear 答案的修改版本。它直接计算周末分钟,而不是计算天数并乘以 24*60。它还解释了周六/周日开始/结束的所有 4 种组合

CONVERT(date,@StartDate)用来获取时间为 的日期,@StartDate然后00:00:00用于计算部分星期日和星期六。有更好的方法可以做到这一点,但我选择了最简单的方法。

DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME
SET @StartDate = '2013/03/15 23:30:00'
SET @EndDate = '2013/03/17 00:30:00'


SELECT
(   DATEDIFF(MINUTE, @StartDate, @EndDate)
    - ( DATEDIFF(wk, @StartDate,@EndDate)*(2*24*60)
        -- End on Sunday
        -(CASE WHEN DATEPART(dw, @EndDate)  = 1 THEN 24.0*60-DATEDIFF(minute,CONVERT(date,@EndDate),@EndDate) ELSE 0 END)
        -- Start on Saturday
        -(CASE WHEN DATEPART(dw, @StartDate) = 7 THEN DATEDIFF(minute,CONVERT(date,@StartDate),@StartDate) ELSE 0 END)
        -- End on Saturday
        +(CASE WHEN DATEPART(dw, @EndDate)  = 7 THEN DATEDIFF(minute,CONVERT(date,@EndDate),@EndDate) ELSE 0 END)
        -- Start on Saturday
        +(CASE WHEN DATEPART(dw, @StartDate) = 1 THEN 24.0*60-DATEDIFF(minute,CONVERT(date,@StartDate),@StartDate) ELSE 0 END)
    )
)
于 2013-08-05T20:16:09.613 回答
1

这个答案假设您想按分钟排除周末,它也完全基于这个问题的答案:

DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME
SET @StartDate = '2013/03/15 23:30:00'
SET @EndDate = '2013/03/18 00:30:00'


SELECT
   (DATEDIFF(MINUTE, @StartDate, @EndDate))
  -(DATEDIFF(wk, @StartDate, @EndDate) * (2*24*60))
  -(CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN (24*60) ELSE 0 END)
  -(CASE WHEN DATENAME(dw, @EndDate) = 'Saturday' THEN (24*60) ELSE 0 END)
于 2013-03-19T13:45:13.160 回答
0

因此,您确实需要能够处理一些 CASE 语句来处理所有边缘情况。这是我放在一起的一个例子。Numbers 表只是一个计数表,在本例中为 1 到 30。

CREATE TABLE #times (id INT IDENTITY(1,1), start_stamp DATETIME, end_stamp DATETIME)

INSERT INTO #times
        ( 
          start_stamp ,
          end_stamp
        )
SELECT DATEADD(DAY, -2*Number, CURRENT_TIMESTAMP), DATEADD(DAY, -1*Number, CURRENT_TIMESTAMP)
FROM Common.NUMBERS
WHERE Number < 31

SELECT id, start_stamp, end_stamp,
CASE WHEN DATEDIFF(DAY, start_stamp, end_stamp) < 7 THEN
    CASE WHEN DATEPART(weekday, start_stamp) < DATEPART(weekday, end_stamp)
        THEN DATEDIFF(MINUTE, start_stamp, DATEADD(HOUR, -48, end_stamp))
        ELSE DATEDIFF(MINUTE, start_stamp, end_stamp) END
    ELSE DATEDIFF(MINUTE, start_stamp, DATEADD(HOUR, -48*(DATEDIFF(WEEK, start_stamp, end_stamp)), end_stamp)) END
    + CASE WHEN DATENAME(weekday,start_stamp) IN ('Sunday', 'Saturday') THEN 1440 ELSE 0 END
    + CASE WHEN DATENAME(weekday,end_stamp) IN ('Sunday', 'Saturday') THEN 1440 ELSE 0 END
FROM #times

可能有一种更优雅的方法可以做到这一点,但代码允许您针对整个结果集运行并按行计算。

于 2013-03-19T14:02:39.847 回答
0
SELECT intwc                             AS wc,
   Datediff(n, start_date, end_date) AS time,
   mh_start_date                     AS date,
   'Repair'                          AS type
FROM   dbo.xxxxxxx 
Where DATEPART(dw, start_date) NOT IN (1, 7) and DATEPART(dw, end_date) NOT IN (1, 7)
于 2013-03-19T13:35:32.293 回答