Never mind.
I was seriously overthinking it.
Faster to work this out in code than in the query.

Thanks all.