the chaining of 5
CTE
s (35 lines). It transpires that the OP is on MySQL 5.6 and my answer wouldn't have worked anyway, but thanks to another poster, I was inspired to produce a
2 liner which does the job AFAICS - no feedback from OP. However, from my complex answer, I was led to ponder the possibility of using SQL in
LIMIT
and
OFFSET
clauses - I see no reason in theory why this should not be possible.