Fix queries for Oracle compatibility

They were using a MySQL only feature in the group by
to prevent duplicates. Do the duplicate filtering
after the database query to keep a single compatible query.

This is a bugfix for 0.9.59 and onwards.