Refactored custom sql for db2