• Thanks for posting CREATE TABLE and INSERT statements, expected results, and the code you have so far - an excellent way to ask a question!

    I copied your code, changed it where needed to run on my case sensitive test server, and got these results:

    CCYY MM Action_Id

    ----------- ----------- -------------------------

    2015 6 NULL

    2015 7 ACT1001

    2015 8 ACT1001

    2015 8 ACT1004

    2015 9 ACT1001

    2015 9 ACT1004

    2015 9 ACT1007

    These are not the same as you posted, but upon inspection of the test data you posted I cannot explain why these would be wrong.

    Why should ACT1001 not be included for months 7 and up? Why should ACT1002 not be?

    Why should ACT1006 be included for months 8? (And not for month 9)

    And finally, why should ACT1009 be included for month 9?

    I interpret backlog actions as actions that have no close date and your query selects on that as well, but based on the expected output that is not the correct interpretation. Can you clarify?


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/