'Build the SQL query on res.partner table to group them according to given criteria
:param fields : list of column names to group by the partners
:param maximum_group : limit of the query'

@api.model
def _generate_query(self, fields, maximum_group=100):

    sql_fields = []
    for field in fields:
        if (field in ['email', 'name']):
            sql_fields.append(('lower(%s)' % field))
        elif (field in ['vat']):
            sql_fields.append(("replace(%s,    '    ',    '')" % field))
        else:
            sql_fields.append(field)
    group_fields = ',    '.join(sql_fields)
    filters = []
    for field in fields:
        if (field in ['email', 'name', 'vat']):
            filters.append((field, 'IS    NOT', 'NULL'))
    criteria = '    AND    '.join((('%s    %s    %s' % (field, operator, value)) for (field, operator, value) in filters))
    text = ['SELECT    min(id),    array_agg(id)', 'FROM    res_partner']
    if criteria:
        text.append(('WHERE    %s' % criteria))
    text.extend([('GROUP    BY    %s' % group_fields), 'HAVING    COUNT(*)    >=    2', 'ORDER    BY    min(id)'])
    if maximum_group:
        text.append(('LIMIT    %s' % maximum_group))
    return '    '.join(text)
