A shit ton of SQL to solve a stupid edge case nobody cared about
Said SQL shitton