fix up SQL query