我需要从两个日期计算中删除公共假日和工作日。我正在运行查询。问题是我对SQL还是很陌生,并且在最后一部分遇到了困难。
我为工作日和公共假期创建了一个功能似乎不错的功能。我只是不知道是谁从DateDiff计算中减去它们。
下面是两位代码。第一个用于函数,第二个用于DateDiff计算。
CREATE FUNCTION [dbo].fn_CountWeekDays --To Create The Function
(
@fromdate Datetime,
@todate Datetime
)
RETURNS TABLE AS RETURN
(SELECT
(DATEDIFF(dd, @fromdate, @todate) + 1)
-(DATEDIFF(wk, @fromdate, @todate) * 2)
-(CASE WHEN DATENAME(dw, @fromdate) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, @todate) = 'Saturday' THEN 1 ELSE 0 END)
-(SELECT COUNT(*) FROM PublicHolidays AS h WHERE h.PublicHolidayDate
BETWEEN @fromdate AND @todate)
As NoOfWeekDays
)
SELECT a.* --the DateDiff
,ISNULL(DATEDIFF(day,b.Last_Action_Date,a.Last_Action_Date),0)
'Date1'
,datediff(day, Min(a.Last_Action_Date) over (partition by a.
[App_ID]),
a.Last_Action_Date) 'Date2'
FROM (select *,ROW_NUMBER() OVER(PARTITION BY [App_ID] ORDER BY
Last_Action_Date)RowRank
from [dbo].[Audit_Log]
)a
LEFT JOIN (select *,ROW_NUMBER() OVER(PARTITION BY [App_ID] ORDER BY
Last_Action_Date)RowRank
from [dbo].[Audit_Log]
)b
ON a.[App_ID] = b.[App_ID]
AND a.RowRank = b.RowRank +1
ORDER BY a.App_ID ASC, a.last_Action_Date ASC
需要从“日期1”和“日期2”中减去“非工作日”
任何帮助将不胜感激。