使用当前财政年度不存在的SQL天数

时间:2012-05-18 08:00:03

标签: sql sql-server sql-server-2008 tsql

我有一张名为'假期表'的表格,其中基本上包含员工不在职的所有日子的日期(例如银行假期等)

以下查询基本上是查看当前的财政年度,并按月计算出可用的天数,然后累计使用所有的会议(例如4月至5月,4月至6月)我不需要4月份虽然我可以使用非累积的。

查看查询:

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

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

1 个答案:

答案 0 :(得分:3)

尝试删除WITH子句中的等号

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

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