I have a report that calculates multiple date differences (in business days, not DATEDIFF) for a variety of business reasons that are far too dull to get into.
Basically the query (right now) looks something like
SELECT -- some kind of information
DATEDIFF(dd, DateOne, DateTwo) AS d1_d2_diff,
DATEDIFF(dd, DateOne, DateThree) AS d1_d3_diff,
DATEDIFF(dd, DateTwo, DateThree) AS d2_d3_diff,
DATEDIFF(dd, DateTwo, DateFour) AS d2_d4_diff
FROM some_table;
I could change this calculation to use a scalar function, but I don't want the scalar function to be executed 4 times for every row in the result set.
I have a Calendar table in the database:
CREATE TABLE Calendar (
Date DATETIME NOT NULL,
IsWeekday BIT,
IsHoliday BIT
);
Would a table-valued function and CROSS APPLY be a good choice here? If so, how would I go about writing such a thing? Or is a scalar function my best bet?
Important Note All date values in our database have been stripped of time so it is safe to ignore any code that would reset days to midnight.