Corrects a bug when using tags and postgresql

Some code related to tags resulted in queries having either

 - ambiguous column names
 - aggregate without some of the fields in the select statement

Those queries could not be executed by postgresql. It's possible
MySQL was not affected as it handles aggregates functions differently.

The queries were modified so that they:

 - use the full column name (table.column) when "column" only is ambiguous
 - use all column specified in "select" in the "group by"