Statistics and sql syntax bug fixes