• I can't see how connecting to the wrong server/database or any server setting or table change can be related to the error the query returns.

    The query itself just has wrong syntax. If you use a "group by" you can't order by a column that is not in your select and not in your group by.