Index: includes/query.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/views/includes/query.inc,v
retrieving revision 1.32
diff -b -u -p -r1.32 query.inc
--- includes/query.inc	28 Oct 2008 20:08:10 -0000	1.32
+++ includes/query.inc	21 Dec 2008 08:57:06 -0000
@@ -890,6 +890,8 @@ class views_query {
       }
     }
 
+    $has_aggregate = FALSE;
+    $non_aggregates = array();
 
     foreach ($fields_array as $field) {
       if ($fields) {
@@ -900,11 +902,18 @@ class views_query {
         $string .= $field['table'] . '.';
       }
       $string .= $field['field'];
+      // store for use with non-aggregates below
+      $fieldname = (!empty($field['alias']) ? $field['alias'] : $string);
+
       if (isset($field['distinct'])) {
         $string = "DISTINCT($string)";
       }
       if (isset($field['count'])) {
         $string = "COUNT($string)";
+        $has_aggregate = TRUE;
+      }
+      else {
+        $non_aggregates[] = $fieldname;
       }
       if ($field['alias']) {
         $string .= " AS $field[alias]";
@@ -917,8 +926,8 @@ class views_query {
       }
     }
 
-    if ($this->groupby) {
-      $groupby = "GROUP BY " . implode(', ', $this->groupby) . "\n";
+    if ($has_aggregate || $this->groupby) {
+      $groupby = "GROUP BY " . implode(', ', array_unique(array_merge($this->groupby, $non_aggregates))) . "\n";
       if ($this->having) {
         $having = $this->condition_sql('having');
       }
