BAC-336: use GROUP BY instead of DISTINCT

See #942