Removed duplicated code for writeSelectGroupBy and writeSelectJoins