Fixed custom mode sql query