我意识到不同的解决方案会有“工作日”的不同变化,但在我的情况下,我的意思是星期一至星期五(含)。计算两个日期之间有多少个工作天 - T-SQL?
基本上我已经创建了一个函数来为我和我目前的解决方案进行计算。我担心(以及提出这个问题的理由)是,我担心这是实现这个目标的一个不好的方法,因为函数被调用的频率非常高。在过去的3个月中,在生产系统上被称为1200万次,平均工人时间为44毫秒。
这让我想知道这是否是实现解决方案的正确方法。
首先这里是我创建的功能:
CREATE FUNCTION [dbo].[fn_WorkDays]
(
@StartDate DATETIME,
@EndDate DATETIME = NULL [email protected] replaced by @StartDate when DEFAULTed
)
RETURNS INT
AS
BEGIN
--===== Declare local variables
--Temporarily holds @EndDate during date reversal
DECLARE @Swap DATETIME
--===== If the Start Date is null, return a NULL and exit
IF @StartDate IS NULL
RETURN NULL
--===== If the End Date is null, populate with Start Date value
-- so will have two dates (required by DATEDIFF below)
IF @EndDate IS NULL
SELECT @EndDate = @StartDate
--===== Strip the time element from both dates (just to be safe) by converting
-- to whole days and back to a date. Usually faster than CONVERT.
-- 0 is a date (01/01/1900 00:00:00.000)
SELECT @StartDate = DATEADD(dd,DATEDIFF(dd,0,@StartDate),0),
@EndDate = DATEADD(dd,DATEDIFF(dd,0,@EndDate) ,0)
--===== If the inputs are in the wrong order, reverse them
IF @StartDate > @EndDate
SELECT @Swap = @EndDate,
@EndDate = @StartDate,
@StartDate = @Swap
--===== Calculate and return the number of workdays using the
-- input parameters. This is the meat of the function.
-- This is really just one formula with a couple of parts
-- that are listed on separate lines for documentation
-- purposes.
RETURN (
SELECT
--Start with total number of days including weekends
(DATEDIFF(dd,@StartDate,@EndDate)+1)
--Subtact 2 days for each full weekend
-(DATEDIFF(wk,@StartDate,@EndDate)*2)
--If StartDate is a Sunday, Subtract 1
-(CASE WHEN DATENAME(dw,@StartDate) = 'Sunday'
THEN 1
ELSE 0
END)
--If EndDate is a Saturday, Subtract 1
-(CASE WHEN DATENAME(dw,@EndDate) = 'Saturday'
THEN 1
ELSE 0
END)
)
END
由于其使用的一个简单的例子,我将运行这种类型的查询:
SELECT MYTABLE.EntryDate
,dbo.fn_WorkDays(MYTABLE.EntryDate, getutcdate()) as WorkingDays
FROM MYTABLE
MyTable的可能含有5000个都具有不同的日期行在EntryDate列(5000调用函数)
我的问题是我在这里错过了一些东西,我这样做,是否有利于创建一个厕所KUP表此(不过这是一个很大的日期组合)
任何想法,改进或建议,将不胜感激......
看看这个答案和比较说明:http://stackoverflow.com/questions/1828948/mysql-function-to-find-the-number-of-working-days-between-two-dates/1828991 #1828991 – 2010-02-13 19:48:48
是否可以添加一个额外字段(计算字段)并仅在数值更改时以天数差异更新它?我的意思是,每次获得差异有什么意义? – shahkalpesh 2010-02-13 20:02:12
始终将存储日期与今天进行比较......因此必须每天更新至少一次(这是一个选项) – 2010-02-13 20:06:06