--- sites/all/modules/views_groupby/handlers/views_groupby_handler_field_groupfields.inc	Mon Feb 01 12:04:03 2010
+++ sites/all/modules/views_groupby/handlers/views_groupby_handler_field_groupfields.inc	Mon Feb 01 12:04:03 2010
@@ -62,7 +62,7 @@
     );
     
     
-    $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',
@@ -272,12 +272,19 @@
         if ( !in_array($key, $to_group) &&
              !in_array($key, $to_aggregate) ) {
 
-             //if ($key != $bfield) {
-             // unset($this->query->fields[$key]);
-             //}             
+//            $this->query->fields[$key]['count'] = TRUE;
+//             if ($key != $bfield) {
+//              unset($this->query->fields[$key]);
+//             }
         } 
         else if (in_array($key, $to_aggregate)) {
-            $this->query->fields[$key][$sql_func] = TRUE;
+            //$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;
         }
       }


	   
--- sites/all/modules/views_groupby/views_groupby.module	Mon Feb 01 12:13:11 2010
+++ sites/all/modules/views_groupby/views_groupby.module	Mon Feb 01 12:13:11 2010
@@ -12,3 +12,84 @@
   );
 }
 
+  /**
+   * 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;
+    }
+    else if ( 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;
+      }
+      else if (!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
+            }
+            else if ($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;
+}
+
+
