Replace clumsy if-else statement with COALESCE()