• Just to add, I don't think anyone is going to give you the SQL to solve this, because the issues are deeper. If you need help, we'll need to work through it one thing at a time. First things first. Do you have ANY room to improve this database format ? If you had arrival times and departure times, you could work out what's a group of connected flights by how close they are together. As it is, you could try, but with some commercial flights taking 15 hours and landing before they left, or missing a day, that is hard.