I’m new to SQL, right off the bat but slowly getting things figured out. I searched but couldn’t find anything that makes sense to me. Like I said, I’m new, I’m sorry. Using SQL Server 2008 R2 if that’s pertinent.
I have one table that’s structured like this
AccountNumber StartDate EndDate
123456 2012-05-07 12:55:55.000 2012-05-09 09:53:55.000
789012 2012-10-17 13:55:55.000 2012-11-02 10:53:55.000
345678 2012-11-13 14:55:55.000 2012-12-14 11:53:55.000
I also have a workdays table that’s structured like this
Date IsWorkday DayOfWeek
2012-01-01 N 1
2012-01-02 N 2
2012-01-03 Y 3
2012-01-04 Y 4
2012-01-05 Y 5
2012-01-06 Y 6
Etc. through 2020-12-31
I didn’t set up the tables and can’t get them changed in any way. I tried, I won’t be allowed. I can’t even email the db admin directly. Neat.
I need select certain columns, which I can handle, but I need one column at the end that will be a count of the number of days between the StartDate and EndDate values, but only if the days in between are in the workdays table with a value of Y for IsWorkday. I hope that makes sense. If it’s simply not possible to do or a fantastically enormous hassle to get it done with the current structure, that’d be good to know too (so I can push for a better workdays/non-workdays table or something). Any help will be much appreciated and may possibly be exchanged for my firstborn.
Try this