Index: views_groupby_handler_field_groupfields.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/views_groupby/handlers/views_groupby_handler_field_groupfields.inc,v
retrieving revision 1.4
diff -u -r1.4 views_groupby_handler_field_groupfields.inc
--- views_groupby_handler_field_groupfields.inc	3 Mar 2009 02:49:05 -0000	1.4
+++ views_groupby_handler_field_groupfields.inc	27 Apr 2010 04:22:51 -0000
@@ -128,7 +128,7 @@
         $field_alias = $val->options['table'] . '_' . $val->options['field'];
         $relationship = '';
 
-          $all_relationships = $this->_get_relationships ();        
+          $all_relationships = $this->_get_relationships ();
           $rel = $val->options['relationship']; 
           $rel = $all_relationships[$rel];
           
@@ -168,7 +168,7 @@
     if (is_array($relationships)) {
         foreach ($relationships as $key => $val) {
             $obj = new stdClass();
-            $obj->fieldprefix = $base_table . '_' . $val['table'];          
+            $obj->fieldprefix = strtolower($val['label']) . '_' . $val['table'];
             $obj->label = $val['label'];
             $obj->table = $val['table'];
             $obj->field = $val['field'];                        
@@ -212,7 +212,25 @@
           $sz_to_group .= ', ';
         }        
         $count++;
-        $sz_to_group .= str_replace('.', '_', $field);
+        if (in_array($field, $this->query->fields)) {
+          $sz_to_group .= str_replace('.', '_', $field);
+        }
+        else {
+          // Find the matching field
+          foreach ($this->query->fields as $field_key => $field_def) {
+            $pattern = '/'. $field_key .'$/';
+            if (preg_match($pattern, $field)) {
+              $to_group[$field_key] = $field_key;
+              if (!empty($field_def['table'])) {
+                $sz_to_group .= $field_def['table'] .'.'. $field_def['field'];
+              }
+              else {
+                $sz_to_group .= '('. $field_def['field'] .')';
+              }
+              break;
+            }
+          }
+        }
       }
       $this->query->add_groupby($sz_to_group);    
     }

