--- handlers/views_groupby_handler_field_groupfields.inc	2009-10-23 15:43:33.000000000 -0500
+++ /tmp/views_groupby_handler_field_groupfields.inc	2010-11-18 21:55:32.986294562 -0500
@@ -62,7 +62,8 @@
     );
     
     
-    $options_sql_func = array( 'count' => t('Count') );
+    $options_sql_func = array( 'count' => t('Count'), 'sum' => t('Sum'), 'avg' => t('Average') );
+
     $form['views_groupby_sql_function'] = array(
       '#title' => t('SQL Aggregation Function'),
       '#type' => 'select',
@@ -276,8 +277,14 @@
              //}             
         } 
         else if (in_array($key, $to_aggregate)) {
-            $this->query->fields[$key][$sql_func] = TRUE;
-            $this->query->fields[$key]['alias'] = $key;
+          //$this->query->fields[$key][$sql_func] = TRUE;
+
+          //To tell Views that some aggregation is taking place (not necessarily COUONT )
+          //The real aggregation is determend in hook_views_query_substitutions implementationn
+          //in views_groupby.module file
+          $this->query->fields[$key]['count'] = TRUE;
+
+          $this->query->fields[$key]['alias'] = $key;
         }
       }
     }
--- views_groupby.module	2009-10-23 15:32:20.000000000 -0500
+++ /tmp/views_groupby.module	2010-11-18 21:54:03.966518175 -0500
@@ -12,3 +12,74 @@
   );
 }
 
+/**
+ * Returns an associative array of string values to be replaced in a query
+ * Replacement is done in a "value replaces key" method
+ *
+ * Query array in view object is replaced for convenience, it doesn't have
+ * any significance on the actual query being executed
+ *
+ * param object &$view
+ * 
+ */
+function views_groupby_views_query_substitutions($view) {
+  $group_by_aliases = $view->query->groupby;
+  $groupby_str = "";
+  $non_aggregates = array();
+  $has_aggregate = false;
+  $group_by = array();
+  foreach ($group_by_aliases as $groupby_field) {
+    $group_by[] = $view->query->fields[$groupby_field]['table'] . "." . $view->query->fields[$groupby_field]['field'];
+  }
+  if (!isset($group_by) || count($group_by) == 0) {
+    return;
+  }
+  elseif (count($group_by) == 1) {
+    $groupby_str = "GROUP BY " . $group_by[0];
+  }
+  else {
+    $groupby_str = "GROUP BY " . implode($group_by, ", ");
+  }
+  $fields_array = $view->query->fields;
+  foreach ($fields_array as $field) {
+    $string = '';
+    if (!empty($field['table'])) {
+      $string .= $field['table'] . '.';
+    }
+    $string .= $field['field'];
+    $fieldname = (!empty($field['alias']) ? $field['alias'] : $string);
+    if (!empty($field['count'])) {
+      $string = "COUNT($string)";
+      $has_aggregate = TRUE;
+    }
+    elseif (!empty($field['aggregate'])) {
+      $has_aggregate = TRUE;
+    }
+    else {
+      $non_aggregates[] = $fieldname;
+    }
+  }
+  $replacements = array();
+  if ($has_aggregate || $view->query->groupby) {
+    $groupby = "GROUP BY " . implode(', ', array_unique(array_merge($view->query->groupby, $non_aggregates)));
+    $replacements[$groupby] = $groupby_str;
+    $view->build_info['query'] = str_replace($groupby, $groupby_str, $view->build_info['query']);
+  }
+  foreach ($view->display as $display) {
+    if (isset($display->display_options['fields']['views_sql_groupedfields']['views_groupby_sql_function'])) {
+      if ($display->display_options['fields']['views_sql_groupedfields']['views_groupby_sql_function'] == "sum") {
+        $replacements['COUNT'] = "SUM";
+        $view->build_info['query'] = str_replace("COUNT", "SUM", $view->build_info['query']);
+        break;
+        // only one aggregation function per query
+      }
+      elseif ($display->display_options['fields']['views_sql_groupedfields']['views_groupby_sql_function'] == "avg") {
+        $replacements['COUNT'] = "AVG";
+        $view->build_info['query'] = str_replace("COUNT", "AVG", $view->build_info['query']);
+        break;
+        // only one aggregation function per query
+      }
+    }
+  }
+  return $replacements;
+}
\ No newline at end of file
