Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7955601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:45:04+00:00 2026-06-04T03:45:04+00:00

I have a table called ‘holiday table’ which basically contains dates for all days

  • 0

I have a table called ‘holiday table’ which basically contains dates for all days where employees will not be in work (e.g bank holidays etc)

The below query is basically looking at the current financial year and working out how many days are available firstly by month, and then using the unuion all cummulatively, (e.g April-May, April-June) I dont need one for April though as I can use the non-cumulative for this.

See query:

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

The results for this are as follows:

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

My problem is when I get to the cumulative, it does another ‘April’ and then at the bottom it does an ‘April-April’ I do not need a cumulative for April as it is only one month do basically I dont want the first or last cumulative values as April is covered by the non-cumulatives, or if the second ‘April’ must stay, then it should not read ‘1’ as days available, by be the same as the non-cumulative, which is 19 as this is how many days are actually available.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T03:45:05+00:00Added an answer on June 4, 2026 at 3:45 am

    Try removing the equals in your WITH clause

    Change WHERE [Date] <= @Enddate to WHERE [Date] < @Enddate

    It seems your adding a day to the date before the WHERE clause therefore it is overstepping by a day.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have table called as Support, which have a field named Name and contains
i have table called type which contains data family,individual i want in some pages
I have table called FinalForgotten which only contains one field called aname. The field
I have a table called OffDays, where weekends and holiday dates are kept. I
We have a table called table1 ... (c1 int indentity,c2 datetime not null,c3 varchar(50)
I have a table called scheduler_sched which has several columns, including a column called
I have table called page which represents every single page in my website. page_id
Hi I have table called Sold Products which stores buying information for product &
I have table in my SQL Server database called OrderDetail which stores the order
I have a table called wp-posts with a field post-content. This field contains the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.