but it didn't work
My query (with changed column names) against the new sample produced the results shown.
run for almost 1 hours
This solution will not scale well as the joining the cte will cause table scans, the more data there is the longer it will take.
If this is a one off query then the time does not matter but if it is a regularly used query I would suggest changing the design to add the last transaction datetime or calculated time difference each time a row is inserted
Far away is close at hand in the images of elsewhere.
Anon.