How to revert joined data back into a hierarchy