Fix pitiful sql