• So you include the whole history each month? Or why do you have only "enddate < @d1"?

    This is an example of filtering using 6 months.

    SELECT *

    FROM SomeTable st

    JOIN (VALUES(0),(-1),(-2),(-3),(-4),(-5)) Tally(n) ON st.enddate = DATEADD(MONTH, Tally.n, @d1)

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2