Fix logic in DB2 SQL