1

我有一个名为“假期表”的表格,它基本上包含员工不上班的所有日期(例如银行假期等)

下面的查询基本上是查看当前财政年度并计算出每月有多少天可用,然后累计使用工会,(例如四月至五月,四月至六月)虽然我不需要一个四月可以为此使用非累积。

见查询:

DECLARE @StartDate DATETIME, 
        @EndDate   DATETIME 

        --available days            
--current – start of this financial year
SELECT @StartDate = (select
case when month(getdate()) >= 4 then
convert(datetime, cast(year(getdate()) as varchar) + '-4-1')
else
convert(datetime, cast(year(getdate())-1 as varchar) + '-4-1')
end), 

--current – end of this financial year
       @EndDate = (select
case when month(getdate()) < 4 then
convert(datetime, cast(year(getdate()) as varchar) + '-3-31')
else
convert(datetime, cast(year(getdate())+1 as varchar) + '-3-31')
end) 


CREATE TABLE #data 
  ( 
     firstday    DATETIME NOT NULL PRIMARY KEY, 
     workingdays INT NOT NULL 
  ); 

WITH dayscte ([Date]) 
     AS (SELECT @StartDate 
         UNION ALL 
         SELECT Dateadd(DAY, 1, [Date]) 
         FROM   dayscte 
         WHERE  [Date] <= @Enddate) 
INSERT INTO #data 
SELECT MIN([Date]), 
       COUNT(*) [Day] 


FROM   dayscte 
       LEFT JOIN dbo.Holiday_Table
         ON [Date] BETWEEN dbo.Holiday_Table.sch_cal_d AND dbo.Holiday_Table.sch_cal_ed 



where
NOT EXISTS (
    SELECT sch_id,sch_cal_d,sch_cal_ed FROM dbo.Holiday_Table WHERE 
    sch_id ='66291100Ks'
    AND
    [date] <= sch_cal_d  
    AND
    [date] >= sch_cal_ed  
    )
       AND Datename(weekday, [Date]) NOT IN ( 'Saturday', 'Sunday' ) 
GROUP  BY Datepart(MONTH, [Date]), 
          Datepart(YEAR, [Date]) 
OPTION (MAXRECURSION 366) 

DECLARE @Date DATETIME 

SET @Date = (SELECT MIN(firstday) 
             FROM   #data) 

SELECT Period, 
       workingdays [Days_Available_Minus_Holidays] ,

       year (firstday) AS [Year]


FROM   (SELECT Datename(MONTH, firstday) [Period], 
               workingdays, 
               0                         [SortField], 
               firstday 
        FROM   #data 


       UNION 
        SELECT Datename(MONTH, @Date) + '-' + Datename(MONTH, firstday), 
               (SELECT SUM(workingdays) 
                FROM   #data b 
                WHERE  b.firstday <= a.firstday ) [WorkingDays], 
               1                                 [SortField], 
               firstday 
        FROM   #data a 
        WHERE  

        firstday > @Date) data 

ORDER  BY sortfield, 
          firstday 

DROP TABLE #data  

GO

结果如下:

Period  Days_Available_Minus_Holidays   Year
April                     19    2012
May                   22    2012
June                      19    2012
July                      22    2012
August           22 2012
September            20 2012
October          23 2012
November             22 2012
December             19 2012
January          23 2013
February             20 2013
March                     21    2013
April                      1    2013
April-May            41 2012
April-June           60 2012
April-July           82 2012
April-August             104    2012
April-September 124 2012
April-October            147    2012
April-November  169 2012
April-December  188 2012
April-January            211    2013
April-February           231    2013
April-March          252    2013
April-April          253    2013

我的问题是当我达到累积值时,它会执行另一个“四月”,然后在底部执行“四月至四月”我不需要四月的累积,因为它只有一个月,基本上我不想要第一个或最后一个累积值,因为 4 月被非累积值覆盖,或者如果第二个“四月”必须保留,那么它不应该读为“1”作为可用天数,与非累积值相同,即 19 为这是实际可用的天数。

4

1 回答 1

3

尝试删除 WITH 子句中的等号

更改WHERE [Date] <= @EnddateWHERE [Date] < @Enddate

似乎您在 WHERE 子句之前的日期上增加了一天,因此它超过了一天。

于 2012-05-18T08:36:34.260 回答