fix queries to be more SQL92-compliant

Specifically we need to avoid grouping by expressions, or selecting
non-grouped non-aggregated columns. This way the queries should be
executable in Teiid.

Bug: 193142
Change-Id: I0858f4bc9b6e7a4992528cecc17685b92fe4bb18