다른 해결책은 "근무일"이 의미하는 바리에이션이 다를 수 있지만 내 경우에는 월요일부터 금요일까지를 의미합니다.2 일 간의 TIP 작업 일수 계산 - T-SQL?
기본적으로 내가 계산을 수행하는 기능을 생성했으며 현재 솔루션이 작동합니다. 내 관심사 (그리고이 질문을하는 이유)는 함수가 매우 높은 빈도로 호출 되었기 때문에 이것이 이것을 달성하는 나쁜 방법이라고 걱정한다. 지난 3 개월 동안 평균 생산 시간은 44ms 인 생산 시스템에서 1,200 만 회라고합니다.
이것이 해결책을 얻는 올바른 방법인지 궁금합니다.
SELECT MYTABLE.EntryDate
,dbo.fn_WorkDays(MYTABLE.EntryDate, getutcdate()) as WorkingDays
FROM MYTABLE
을 MyTable 모든 다른 날짜로 5000 개 행을 포함 할 수있다 : 나는 이러한 유형의 쿼리를 실행하는 것입니다 그것의 사용의 간단한 예를 들어
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
: 첫째로 여기
은 내가 만든 기능입니다 EntryDate 칼럼 (함수에 대한 5000 호출)
내 질문은 내가 이것을하고있는 방식으로 여기에 뭔가 빠져있다. loo를 만드는 것이 유익 할까? 이것에 대한 KUP 테이블
어떤 생각, 개선 또는 권고 감상 할 수있다 (하지만 그 날짜의 조합의 많은입니다) ... 여기에 두 가지 문제가있다
이 답변을보고 메모를 비교해보십시오. http://stackoverflow.com/questions/1828948/mysql-function-to-find-the-number-of-working-days-between-two-dates/1828991 # 1828991 –
추가 필드 (계산 된 필드)를 추가하고 값이 변경된 경우에만 일의 차이를 업데이트 할 수 있습니까? 나는 매번 차이점을 얻는 것이 무엇을 의미합니까? – shahkalpesh
항상 저장된 날짜와 오늘을 비교하면 ... 하루에 적어도 한 번 업데이트해야합니다 (옵션 임) –