• It's probably due to some funky behavior with the two ROW_NUMBER() functions and ties in the data set (I remember there being some funkiness there).

    What's the result of the following query against the actual data (for one of the missing YearMonth values)?

    SELECT Ascending_RN = ROW_NUMBER() OVER ( PARTITION BY YearMonth ORDER BY LOSInMinutes ASC ) ,

    Descending_RN = ROW_NUMBER() OVER ( PARTITION BY YearMonth ORDER BY LOSInMinutes DESC ) ,

    YearMonth ,

    LOSInMinutes

    FROM #TEST

    WHERE YearMonth='2015-02'

    You might want to use the same approach but with the 2005_B method from that article I cited. It should be free of any problems with ties.

    Cheers!