问题
I have three tables, One of which is a master calendar that houses an indicator for company Holidays. I would like to exlude Holiday from my DateDiff calculation. Below is what I have...
alert date 12/23/16 complete date 12/28/16 company Holiday 12/26/16 Need to exlude from DateDIff calculation
--My calculation shows 3 business days it should exclude 12/26/16 = 2 business days
DATEDIFF(DD,A.ALERTS_CREATE_DT,S.CreatedDate)
-(DATEDIFF(WK,A.ALERTS_CREATE_DT,S.CreatedDate) * 2) --HOW MANY WEEKEND DAYS PASSED BY
-(CASE WHEN DATENAME(DW,A.ALERTS_CREATE_DT) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(DW,S.CreatedDate) = 'Saturday' THEN 1 ELSE 0 END)
-(CASE WHEN CAL.GRH_HOLIDAY_IND = 'Y' THEN 1 ELSE 0 END)
-( SELECT COUNT(CAL.GRH_HOLIDAY_IND)
FROM A
FULL OUTER JOIN S ON A.ID= S.ID
INNER JOIN CAL ON A.ALERTCREATEDT_MMDDYYYY = CAL.CALENDAR_DATE
WHERE 1=1
AND Cal.GRH_HOLIDAY_IND = 'Y'
AND CAL.CALENDAR_DATE between CAST(A.CREATE_DT AS DATE) AND CAST(S.CreatedDate AS DATE) ) AS BusinessDays
Trying to remove the Holidays is where I'm struggling, see below.
-( SELECT COUNT(CAL.GRH_HOLIDAY_IND)
FROM A
FULL OUTER JOIN S ON A.ID= S.ID
INNER JOIN CAL ON A.ALERTCREATEDT_MMDDYYYY = CAL.CALENDAR_DATE
WHERE 1=1
AND Cal.GRH_HOLIDAY_IND = 'Y'
AND CAL.CALENDAR_DATE between CAST(A.CREATE_DT AS DATE) AND CAST(S.CreatedDate AS DATE
回答1:
After 2 case statements you can add below statement to subtract no of days from holiday Calendar table.
- (select ISNULL(count(1),0) from Cal where Cal.Holiday between ALERTS_CREATE_DT AND CreatedDate )
来源:https://stackoverflow.com/questions/42721702/sql-server-2008-exclude-company-holiday-from-datediff