[英]SQL - datediff (minutes) exclude weekends
can you help me, I´m using below sql view(which then I´m using in crystal reports). 您能帮我吗,我正在使用下面的sql视图(然后在Crystal报表中使用了它)。 I need there date differences(in minutes), but now I need to exclude weekends.
我需要有日期差异(以分钟为单位),但是现在我需要排除周末。 Please help :)
请帮忙 :)
SELECT intwc AS wc,
Datediff(n, start_date, end_date) AS time,
mh_start_date AS date,
'Repair' AS type
FROM dbo.xxxxxxx
This is a modifed version of @bendataclear's answer. 这是@bendataclear答案的修改版本。 It calculates weekend minutes directly rather than caculating the days and multiplying by 24*60.
它直接计算周末分钟数,而不是计算天数并乘以24 * 60。 It also accounts for all 4 combinations of starting/ending on saturday/sunday
它还考虑了周六/周日开始/结束的所有4种组合
I'm using CONVERT(date,@StartDate)
to get the date of @StartDate
with a time of 00:00:00
, which is then used to calculate the partial Sundays and Saturdays. 我正在使用
CONVERT(date,@StartDate)
来获取@StartDate
的日期,其时间为00:00:00
,然后将其用于计算部分星期日和星期六。 There are better ways of doing that but I went with the simplest. 有更好的方法可以做到这一点,但我最简单。
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)
)
)
This answer assumes you want to exclude weekends on a minute basis, also it's based entirely on the answer in this question : 这个答案假设您想在一分钟内排除周末,这也完全基于此问题的答案:
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)
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)
So you do need to be able to juggle some CASE statements to handle all of your edge cases. 因此,您确实需要能够处理一些CASE语句来处理所有的极端情况。 Here's an example that I put together.
这是我整理的一个示例。 The Numbers table is just a tally table, in this case 1 through 30.
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
There may be a more elegant way to do that, but the code allows you to run against the entire result set and calculate per row. 可能有一种更优雅的方法,但是代码允许您针对整个结果集运行并按行计算。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.