• Have you considered a pure (ish!) SQL solution?

    Haven't got much time right now, but have a look here[/url] & see whether it might be useful.