bug fixes for the special cases in the SQL statement.