I have a calendar table which has columns for the date and whether or not it is a working day as defined in our ERP system. What I need help with is working out the working day numbers for the dates. I need to get:
WRKDAY - The number of the working day in the month, so excluding non-work days.
REMAIN - The number of working days remaining in the month, again ignoring non-work days
WRKTOTAL - The number of working days in the week in total, ignoring non-work days
This is what I need to get to using January 2016 as an example:
+---------+------------+---------+-----------+--------+--------+----------+
| CALCODE | CALDATE | CALWORK | CALNAME | WRKDAY | REMAIN | WRKTOTAL |
+---------+------------+---------+-----------+--------+--------+----------+
| BH | 01/01/2016 | 0 | Friday | | | |
| BH | 02/01/2016 | 0 | Saturday | | | |
| BH | 03/01/2016 | 0 | Sunday | | | |
| BH | 04/01/2016 | 1 | Monday | 1 | 20 | 5 |
| BH | 05/01/2016 | 1 | Tuesday | 2 | 19 | 5 |
| BH | 06/01/2016 | 1 | Wednesday | 3 | 18 | 5 |
| BH | 07/01/2016 | 1 | Thursday | 4 | 17 | 5 |
| BH | 08/01/2016 | 1 | Friday | 5 | 16 | 5 |
| BH | 09/01/2016 | 0 | Saturday | | | |
| BH | 10/01/2016 | 0 | Sunday | | | |
| BH | 11/01/2016 | 1 | Monday | 6 | 15 | 5 |
| BH | 12/01/2016 | 1 | Tuesday | 7 | 14 | 5 |
| BH | 13/01/2016 | 1 | Wednesday | 8 | 13 | 5 |
| BH | 14/01/2016 | 1 | Thursday | 9 | 12 | 5 |
| BH | 15/01/2016 | 1 | Friday | 10 | 11 | 5 |
| BH | 16/01/2016 | 0 | Saturday | | | |
| BH | 17/01/2016 | 0 | Sunday | | | |
| BH | 18/01/2016 | 1 | Monday | 11 | 10 | 5 |
| BH | 19/01/2016 | 1 | Tuesday | 12 | 9 | 5 |
| BH | 20/01/2016 | 1 | Wednesday | 13 | 8 | 5 |
| BH | 21/01/2016 | 1 | Thursday | 14 | 7 | 5 |
| BH | 22/01/2016 | 1 | Friday | 15 | 6 | 5 |
| BH | 23/01/2016 | 0 | Saturday | | | |
| BH | 24/01/2016 | 0 | Sunday | | | |
| BH | 25/01/2016 | 1 | Monday | 16 | 5 | 5 |
| BH | 26/01/2016 | 1 | Tuesday | 17 | 4 | 5 |
| BH | 27/01/2016 | 1 | Wednesday | 18 | 3 | 5 |
| BH | 28/01/2016 | 1 | Thursday | 19 | 2 | 5 |
| BH | 29/01/2016 | 1 | Friday | 20 | 1 | 5 |
| BH | 30/01/2016 | 0 | Saturday | | | |
| BH | 31/01/2016 | 0 | Sunday | | | |
+---------+------------+---------+-----------+--------+--------+----------+
I've tried using DATEPART, but can't get to the numbers in the places I need them, so any help would be appreciated.