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
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.