I didn't occur to me to try CROSS APPLY (though it has many uses), when I saw the pattern, the first thing that came to me was recursion.
I didn't reply for two reasons. Had to leave for a few hours, but I wanted to see what someone would come up with (and figured it would be you)
That is not a simply query - and you got it. More than Jedi enough for me - I'm convinced. I'm going to nominate you.