• I am not going to blame the problem on a bug in SQL Server. I am more likely to say there is a logic/data error somewhere in the code.

    My suggestion at this point is rewrite the query doing it in steps making sure each step works as you move to the next until you have rewritten the query. Doing this you may find what is causing the error.