Using groupby in hook_views_query_alter() I have a view that is using a format that doesn't include group by fields in its settings. I'm trying to mimic that by using the hook_views_query_alter() function in a module.

I know the code is running, because I see the DPM value and the error log string, but the group by doesn't seem to be working. I know that group by in views is a different thing than mySQL's group_concat (which you can use via aggregation, but that doesn't work in this case because we need one field to not be aggregated.) So I'm not expecting to see a group_concat in the printed-out query. But I would have expected it to produce the same results as if I had set up Group By in unformatted list settings for the same four fields (which does work as expected.)

Here is what my view looks like without any group-by (either in the settings or the module):



You can see that there are two complete chunks of information for April Carlisle, because she is speaking at two sessions.

If I turn on Group By for speaker name (content: title), job title, and company in unformatted list settings, this is what I get (and what I want):



So I had thought that my module code would reproduce that result, but it's not. Can anyone tell me why?

Here's the main function:

function group_speaker_carousel_views_query_alter(&amp;$view, &amp;$query) {
  switch($view-&gt;name) {
    case 'speakers_carousel':  
      error_log("view name now: ".$view-&gt;name);
      $query-&gt;groupby[0]['field'] = "title";
      $query-&gt;groupby[1]['field'] = "field_job_title";
      $query-&gt;groupby[2]['field'] = "field_company";

      $select_query = $query-&gt;query();
      $query_string = (string)$select_query;
      dpm($query_string);
      break;
  }
}
"Group By" is not working on hook_views_query_alter and it is views module issue. that is reported at https://drupal.org/node/590534, http://drupal.org/node/1608742, http://drupal.org/node/389230#comment-2637200

So, You should use other hooks such as general hook_query_alter.

function hook_query_alter( &amp;$query) {

  if($query-&gt;alterMetaData['view']-&gt;name == "your_views_name"){
     $query-&gt;groupBy("domain_source.domain_id");
     //also you can unset other group by critera by using, unset($query-&gt;getGroupBy());
  }
}