简体   繁体   中英

Find 2 Business Days

I have a Calendar table. I would like get 2 business before Today's Date while skipping Holidays and weekeends.

For example:

Example 1: New years 2013 falls on a Tuesday. If it was Thursday (1/3), then my starting date would be Monday (12/31).

Example 2: If it was Monday (12/31), then my starting date would be Thursday (12/27).

My Clandar table as been prepopulated with 30 years worth of dates and have flags to determine which ones are weekend and hollidays. The table has these fields:

dt: Date and time for 30 years Y: year D: Day M: Month Isholiday: (Is a holiday are not, flagged with 0 for "no" and 1 for "Yes") IsWeekday: (Is a weekday? Flagged with 0 for "no" and 1 for "Yes" HolidayDescritption: Holiday Names

Please help.

This is what you want :

select top 1 *
  from ( select * 
           from Calendar 
          where IsHoliday = 0 
            and IsWeekday = 1 
            and dt between DateAdd( Day, -6, GetDate ) and GetDate() 
       ) tmpCalendar
 where dt <= DateAdd( Day, -2, GetDate() )
 order by dt desc

Make sure to replace * with only the fields that you want.

Try this one:

SELECT dt FROM
(
    SELECT dt, ROW_NUMBER() OVER (ORDER BY dt DESC) AS R
    FROM
        Calendar
    WHERE
        IsHoliday = 0
        AND IsWeekday = 1
        AND dt < GETDATE()
) T
WHERE R = 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM