我想从特定时间段获得查询结果。我想要做的是在周六运行一份报告,该报告将从当周的周一上午7点到本周五的周五下午5点(工作时间)返回结果。 我能够做到这一点:
BETWEEN DATEADD(wk, DATEDIFF(wk, 7, GETDATE()), 7)
AND DATEADD(wk, DATEDIFF(wk, 11, GETDATE()), 11)
然而,这只是日子。我该如何添加时间?
提前致谢!
更新: 我想知道你是否可以最后一次帮助我。我将日期信息添加到查询中,但我得到的结果大约是我期望的5倍。让我发布整个代码,这样你就可以看到我在做什么:
DECLARE @inc INT, @out INT
SET @inc = (SELECT COUNT(SessionID) FROM altigen.dbo.CDRMAIN WHERE
DATEADD(ss, CAST((StartTime - 28800) AS INT), '1970-01-01') BETWEEN DATEADD(hh, 7, DATEADD(wk, DATEDIFF(wk, 7, GETDATE()), 7)) AND DATEADD(hh, 17, DATEADD(wk, DATEDIFF(wk, 7, GETDATE()), 7))
OR DATEADD(ss, CAST((StartTime - 28800) AS INT), '1970-01-01') BETWEEN DATEADD(hh, 7, DATEADD(wk, DATEDIFF(wk, 8, GETDATE()), 8)) AND DATEADD(hh, 17, DATEADD(wk, DATEDIFF(wk, 8, GETDATE()), 8))
OR DATEADD(ss, CAST((StartTime - 28800) AS INT), '1970-01-01') BETWEEN DATEADD(hh, 7, DATEADD(wk, DATEDIFF(wk, 9, GETDATE()), 9)) AND DATEADD(hh, 17, DATEADD(wk, DATEDIFF(wk, 9, GETDATE()), 9))
OR DATEADD(ss, CAST((StartTime - 28800) AS INT), '1970-01-01') BETWEEN DATEADD(hh, 7, DATEADD(wk, DATEDIFF(wk, 10, GETDATE()), 10)) AND DATEADD(hh, 17, DATEADD(wk, DATEDIFF(wk, 10, GETDATE()), 10))
OR DATEADD(ss, CAST((StartTime - 28800) AS INT), '1970-01-01') BETWEEN DATEADD(hh, 7, DATEADD(wk, DATEDIFF(wk, 11, GETDATE()), 11)) AND DATEADD(hh, 17, DATEADD(wk, DATEDIFF(wk, 11, GETDATE()), 11))
AND Direction = 1 AND VMDuration = 0 AND TargetWGNum IN ('200','201','210','211','212','240'))
SET @out = (SELECT COUNT(SessionID) FROM altigen.dbo.CDRMAIN WHERE
DATEADD(ss, CAST((StartTime - 28800) AS INT), '1970-01-01') BETWEEN DATEADD(hh, 7, DATEADD(wk, DATEDIFF(wk, 7, GETDATE()), 7)) AND DATEADD(hh, 17, DATEADD(wk, DATEDIFF(wk, 7, GETDATE()), 7))
OR DATEADD(ss, CAST((StartTime - 28800) AS INT), '1970-01-01') BETWEEN DATEADD(hh, 7, DATEADD(wk, DATEDIFF(wk, 8, GETDATE()), 8)) AND DATEADD(hh, 17, DATEADD(wk, DATEDIFF(wk, 8, GETDATE()), 8))
OR DATEADD(ss, CAST((StartTime - 28800) AS INT), '1970-01-01') BETWEEN DATEADD(hh, 7, DATEADD(wk, DATEDIFF(wk, 9, GETDATE()), 9)) AND DATEADD(hh, 17, DATEADD(wk, DATEDIFF(wk, 9, GETDATE()), 9))
OR DATEADD(ss, CAST((StartTime - 28800) AS INT), '1970-01-01') BETWEEN DATEADD(hh, 7, DATEADD(wk, DATEDIFF(wk, 10, GETDATE()), 10)) AND DATEADD(hh, 17, DATEADD(wk, DATEDIFF(wk, 10, GETDATE()), 10))
OR DATEADD(ss, CAST((StartTime - 28800) AS INT), '1970-01-01') BETWEEN DATEADD(hh, 7, DATEADD(wk, DATEDIFF(wk, 11, GETDATE()), 11)) AND DATEADD(hh, 17, DATEADD(wk, DATEDIFF(wk, 11, GETDATE()), 11))
AND Direction = 2 AND VMDuration = 0 AND TargetWGNum = 0 AND LEN(TargetNum) > 3)
SELECT @inc + @out AS TotalCalls
基本上,这是查询呼叫数据库以获取工作时间(周一至周五)之间的来电和去电总数。
也许有更简单,更好的方法,或者只是我忽略的东西?本周,我预计会有几百个电话,而我的查询结果将近2,000个。 谢谢!
答案 0 :(得分:1)
只需添加小时数:
BETWEEN DATEADD(hh, 7, DATEADD(wk, DATEDIFF(wk, 7, GETDATE()), 7))
AND DATEADD(hh, 17, DATEADD(wk, DATEDIFF(wk, 11, GETDATE()), 11))
如果您需要在每天的工作时间内获得结果,则需要单独设置时间范围:
myDate BETWEEN DATEADD(hh, 7, DATEADD(wk, DATEDIFF(wk, 7, GETDATE()), 7))
AND DATEADD(hh, 17, DATEADD(wk, DATEDIFF(wk, 7, GETDATE()), 7)) OR
myDate BETWEEN DATEADD(hh, 7, DATEADD(wk, DATEDIFF(wk, 8, GETDATE()), 8))
AND DATEADD(hh, 17, DATEADD(wk, DATEDIFF(wk, 8, GETDATE()), 8)) OR
myDate BETWEEN DATEADD(hh, 7, DATEADD(wk, DATEDIFF(wk, 9, GETDATE()), 9))
AND DATEADD(hh, 17, DATEADD(wk, DATEDIFF(wk, 8, GETDATE()), 8)) etc.
更新:如果您的WHERE子句中的日期/时间条件之后还有其他条件,请不要忘记将OR
运算符括在括号中:
WHERE
(myDate BETWEEN DATEADD(hh, 7, DATEADD(wk, DATEDIFF(wk, 7, GETDATE()), 7))
AND DATEADD(hh, 17, DATEADD(wk, DATEDIFF(wk, 7, GETDATE()), 7)) OR
myDate BETWEEN DATEADD(hh, 7, DATEADD(wk, DATEDIFF(wk, 8, GETDATE()), 8))
AND DATEADD(hh, 17, DATEADD(wk, DATEDIFF(wk, 8, GETDATE()), 8)) OR
myDate BETWEEN DATEADD(hh, 7, DATEADD(wk, DATEDIFF(wk, 9, GETDATE()), 9))
AND DATEADD(hh, 17, DATEADD(wk, DATEDIFF(wk, 8, GETDATE()), 8)) etc.
) AND Direction = 1 AND VMDuration = 0 AND ... etc.
了解更多信息,了解SQL Server运算符优先级here