'Get the SQL to properly select a per-group count of unique matches for
a grouped query.  Returns a tuple (field alias, field SQL)'

def get_count_sql(self, query):

    if query.query.distinct:
        pk_field = self.get_key_on_this_table()
        count_sql = ('COUNT(DISTINCT    %s)' % pk_field)
    else:
        count_sql = 'COUNT(1)'
    return (self._GROUP_COUNT_NAME, count_sql)
