How to group by one field I have a view which shows the users subscribed to the website. Some users have multiple roles, therefore, on the results, there are some duplicates. 

What I want to do is to remove these duplicates, so I've tried to enable the Aggregation in the view settings. This action reduced the repeated results, but it haven't erased them. 
By looking at the query the GROUP BY is done for every field, but I want to enable for just one field, more specifically like this: GROUP BY users_name.

I've tried to achieve it by making a simple module with this function: 

function alter_users_query_views_query_alter(&amp;$view, &amp;$query) {
    if ($view-&gt;name == 'all_users') {
        $query-&gt;add_groupby('users_name');
    }
}


Which adds the additional field in the GROUP BY, but it doesn't remove the others. 
How can I achieve it? More specifically, how can I unset the other fields and group by just one?
I was able to find a solution by following this example. Here is the code in my case:

function alter_users_query_query_alter($query) {
    if (isset($query-&gt;alterMetaData)) {
        if (isset($query-&gt;alterMetaData['view'])) {
            if($query-&gt;alterMetaData['view']-&gt;name == 'all_users') {
                $fields =&amp; $query-&gt;getGroupBy();

                unset($fields['field-you-want-to-remove']);
                $query-&gt;groupBy('users_name');
            }
        }
    }
}