Howto : Group by in Views 2

hook_views_pre_execute(&$view) This hook is called right before the execute process. The query is now fully built, but it has not yet been run through db_rewrite_sql. Adding output to the view can be accomplished by placing text on $view->attachment_before and $view->attachment_after. So I'm using this to rewrite the SQL and add the GROUP BY which is missing in the views.

Solution : Implement this hook in your module and override it. Here how I did it

* Implements hook_views_pre_execute().
function MODULE_NAME_views_pre_execute(&$view) {
  if ($view->name == 'VIEW_NAME' && ($view->current_display == 'CAN BE/BLOCK/PAGE/DEFAULT')) {
    $count = 0;
    $search = array('ORDER BY');
    $replace = array('GROUP BY GROUP BY node_data_field_membership_type_field_membership_type_value ORDER BY');
    $view->build_info['query'] = str_replace($search,
            $view->build_info['query'], $count);
    // if there are no any order by then add the GROUP BY
    if ($count == 0) {
      $view->build_info['query'] .= ' GROUP BY node_data_field_membership_type_field_membership_type_value';
      $view->build_info['count_query'] .= ' GROUP BY node_data_field_membership_type_field_membership_type_value';
      $view->build_info['count_query'] = str_replace($search,