Well, it's a bit more complicated but I wanted to at least try and sort out the complications myself.
I want to group all rows with the same reference where the date_out between each is no more than 30 days.