Hmm, I would rather do this in the SQL code ...
I scrapped up the following >>
with cte as
(
selectcast('20140701' as date) as cteDate
Union all
selectdateadd(d, 1,c.cteDate)
fromcte as c
wherec.cteDate <'20140731'
)
,nextCte as
(
select top 1
cteDate,
min(cteDate) Over (Partition by NULL) as minDate,
max(cteDate) Over (Partition by NULL) as maxDate
from cte
)
Select
minDate,
maxDate,
datediff(d, minDate, maxDate) as numDaysBetween,
dateadd(d, datediff(d, minDate, maxDate)/2,minDate) as averageDate
from nextCte
If gives me the average day of 7/16 for the month of July. You may need to do some tweaking for when the number of days between divided by two is not a whole number (like employ datetime instead of date). See if this gets you going.
----------------------------------------------------