Filter dates to include holidays and weekends

  • Hello,

    You have to be a little more specific. What is it really you want to obtain as result? And what is your input? From your example query (that I assume does not work as you want it to) it is very hard to try to decipher what you ask us to do...

    Happy hunting,

    Hanslindgren

  • There seems to be something wrong i here i replied thrice already and nothing went through.

     

    I seemed to have solved the problem halfway.

     

    I removed the SELECT from the while loop and put it out side then i only put the criteria set on onside which increments the loop counting days backward until it reaches the first weekday, then it breaks to the query outside.

     

    Only problem is that it now includes one day to much but works fine for normal week days.

     

    Eg.Easter weekend here includes monday and friday as a holiday

    DECLARE @today datetime

    SELECT  @today = '04/17/2006'

    DECLARE @lastWorkingDay datetime

    SELECT @lastWorkingDay ='04/16/2006'

    WHILE    ( (SELECT dbo.CALENDAR.isWeekday

            FROM dbo.CALENDAR

            WHERE CONVERT(varchar(8), dbo.CALENDAR.dt, 112) = CONVERT(varchar(8),@lastWorkingDay, 112)) <>  1  OR

     (SELECT dbo.CALENDAR.isHoliday

            FROM dbo.CALENDAR

            WHERE CONVERT(varchar(8), dbo.CALENDAR.dt, 112) = CONVERT(varchar(8),@lastWorkingDay, 112)) <>  0)

     

    The above code checks if today is not a weekday

    then moves on to increment to the previous day in the loop

    BEGIN

       SET @today = @today-1

       IF ( (SELECT dbo.CALENDAR.isWeekday

            FROM dbo.CALENDAR

            WHERE CONVERT(varchar(8), dbo.CALENDAR.dt, 112) = CONVERT(varchar(8),@today, 112)) =  1  AND

     (SELECT dbo.CALENDAR.isHoliday

            FROM dbo.CALENDAR

            WHERE CONVERT(varchar(8), dbo.CALENDAR.dt, 112) = CONVERT(varchar(8),@today, 112)) =  0)

     

          BREAK

       ELSE

          CONTINUE

    END

    The problem is that the loop needs to break out just before the last working it looped back to, it includes everything from the wed 07:00am before easter which it should not becasue this report is run daily. It was run for wed already the thursday before easter.

    Its kinda difficult to wrap ur mind around this, just remember that if the query is run every working day it includes evrything from previous day 07:00am

    hence:

    SELECT @today

    SELECT     COUNT(*) AS [COUNT],

         dbo.DEC_TXN.CUSTOMER,

         dbo.DEC_TXN.STATUS,

         dbo.STATUS.STATUS_DESC

      FROM       dbo.DEC_TXN INNER JOIN

                            dbo.CALENDAR ON CONVERT(varchar(8), dbo.DEC_TXN.DATE_LOAD, 112) = CONVERT(varchar(8), dbo.CALENDAR.dt, 112) INNER JOIN

                            dbo.STATUS ON dbo.DEC_TXN.STATUS = dbo.STATUS.STATUS

      WHERE     (dbo.DEC_TXN.DATE_LOAD between DATEADD(hh, - 17, CONVERT(datetime, CAST(MONTH(@toDay) AS char(2)) + '/' + CAST(DAY(@today) AS char(2))

                             + '/' + CAST(YEAR(@toDay) AS char(4)), 101))AND GETDATE())

      GROUP BY dbo.DEC_TXN.CUSTOMER, dbo.DEC_TXN.STATUS , dbo.STATUS.STATUS_DESC

      ORDER by dbo.DEC_TXN.CUSTOMER, dbo.DEC_TXN.STATUS

     

     so i now need assistance to play around with either the variables or the timing of the calendar so that the weekends and the holidays are included. 

    it must only start from 7:00am the previous working day if it is a normal working day

    AND

    it must start including records from 7:00am the day before easter Friday (Thursday)

    At the moment my query is including everthing from the previous wed. 07:00am

     


    Kindest Regards,

    Kashief

Viewing 2 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply