计算星期五或星期一是假日的星期六和星期日营业日期

时间:2013-12-06 16:53:35

标签: sql sql-server-2008 date calendar business-rules

我正在使用MS SQL 2008中的日历生成器,需要为该月的每一天生成正确的工作日。一切都在处理这个例外情况:如果周五或周一是假日,那么我在周末(周六和周日)的商业日期会得到错误的价值。我已经有一个列,如果星期一(MHDY)是假日,则返回值1,并且星期五也是一个单独的列(FHldy),我试图做的是在MHDY和FHldy中分配值1星期六和星期日的列,如果星期五或星期一之后是假期。以下是产生上述值的代码部分:

SELECT 
     MC.[Date]
    ,MC.[Year]
    ,MC.[Quarter]
    ,MC.[Month]
    ,MC.[Week]
    ,MC.[Day]
    ,MC.[DayOfYear]
    ,MC.[Weekday] -- This has first day of week as Monday
    ,MC.[KindOfDay]
    ,MC.[Description]
    ,CASE   WHEN Weekday BETWEEN 1 and 6 THEN Weekday +1
            WHEN Weekday = 7 THEN 1 END AS [DayofWeek]
    -- This will convert first day of week to Sunday
    , CASE WHEN MC.KindOfDay = 'Holiday' THEN 1 ELSE 0 END AS Holiday
    , CASE WHEN MC.KindOfDay <> 'BusDay' THEN 1 ELSE 0 END AS NonBDay
    , CASE WHEN MC.KindOfDay = 'BusDay' THEN 1 ELSE 0 END AS BDay
    , CASE WHEN MC.KindOfDay = 'Holiday' AND MC.Weekday = 1 THEN 1 ELSE 0 END AS MHDY
    , CASE WHEN MC.Date = DateAdd(Month, 1, MC.Date - Day(MC.Date) + 1) -1 THEN 1                  ELSE 0 END AS LDoM
    , CASE WHEN MC.KindOfDay = 'Holiday' AND MC.[Weekday] = 5 THEN 1 ELSE 0 END AS FHldy

INTO #AllC2


FROM #MasterCal MC

日历中的代码比此部分多得多,但这是我尝试添加此信息的地方。

有没有办法修改CASE语句说“如果星期一是假日,则将星期六和星期日的值1返回给MHDY”。这同样适用于星期五。

如此接近......这是最后一道障碍。我感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:0)

问题的解决方案变得简单。 我在#MasterCal表中的CASE语句中添加了一行,以便在星期一假期之前将Weekends标记为KindOfDay字段中的Holidays。我在下一节中添加了一行,告诉它分配那些被列为假期的周末的日期。问题解决了。