SQL to return the number of working days between 2 passed in dates

前端 未结 6 1589
礼貌的吻别
礼貌的吻别 2021-01-07 08:10

I need to write an sql query that returns the number of Working days (Monday - Friday) between two given dates.

I was wondering what would be the most efficient way

6条回答
  •  温柔的废话
    2021-01-07 08:36

    This is how i do it, assuming you already got a calendar table with acolumn which indicates if a day is working day or not: Add a new column to your calendar table like workday_num and populate it once with a running number using

    sum(case when workingday then 1 else 0 end)
    over (order by calendardate rows unbounded preceding)
    

    Now it's two joins to your calendar and a simple difference of the workday_nums of p_start_date and p_end_date.

提交回复
热议问题