fixed group by

Fixes Postgres issue reported in AGPUSH-879