diff -r -p views/handlers/views_handler_field.inc views-patched/handlers/views_handler_field.inc
*** views/handlers/views_handler_field.inc	2009-06-03 20:55:52.000000000 +0100
--- views-patched/handlers/views_handler_field.inc	2009-07-22 10:02:20.000000000 +0100
*************** class views_handler_field extends views_
*** 48,53 ****
--- 48,64 ----
      return TRUE;
    }
  
+   /**
+    * Determine if this field can be aggregated over.
+    *
+    * Fields can set this to FALSE if they do not wish to allow
+    * aggregation functions (such as COUNT, SUM, MAX, etc.) to be
+    * used on this field.
+    */
+   function allow_aggregation() {
+     return TRUE;
+   }
+ 
    function init(&$view, $options) {
      parent::init($view, $options);
  
*************** class views_handler_field extends views_
*** 61,69 ****
     */
    function query() {
      $this->ensure_my_table();
!     // Add the field.
      $this->field_alias = $this->query->add_field($this->table_alias, $this->real_field);
  
      $this->add_additional_fields();
    }
  
--- 72,86 ----
     */
    function query() {
      $this->ensure_my_table();
! 
!     // Add the field, taking care of any aggregation that
!     // may affect it.
      $this->field_alias = $this->query->add_field($this->table_alias, $this->real_field);
  
+     if ($this->options['aggregate']['aggregate_field']) {
+       $this->query->fields[$this->field_alias][$this->options['aggregate']['sql_function']] = TRUE;
+     }
+ 
      $this->add_additional_fields();
    }
  
*************** class views_handler_field extends views_
*** 182,187 ****
--- 199,234 ----
        '#description' => t('Check this box to not display this field, but still load it in the view.  Use this option to not show a grouping field in each record, or when doing advanced theming.'),
      );
  
+     if ($this->allow_aggregation()) {
+       $form['aggregate']['#tree'] = TRUE;
+       $form['aggregate']['aggregate_field'] = array(
+         '#type' => 'checkbox',
+         '#title' => t('Aggregate this field'),
+         '#default_value' => $this->options['aggregate']['aggregate_field'],
+         '#description' => t('Check this box if you want to apply an aggregation function (such as a count, average) to the values in this field.'),
+       );
+ 
+       $sql_functions = array(
+         'count' => t('Count'),
+         'sum' => t('Sum'),
+         'average' => t('Average'),
+         'minimum' => t('Minimum'),
+         'maximum' => t('Maximum'),
+       );
+ 
+       $form['aggregate']['sql_function'] = array(
+         '#type' => 'select',
+         '#title' => t('Aggregation function'),
+         '#default_value' => $this->options['aggregate']['sql_function'],
+         '#description' => t('The function that should be applied to the values in this field.'),
+         '#options' => $sql_functions,
+         '#process' => array('views_process_dependency'),
+         '#dependency' => array(
+           'edit-options-aggregate-aggregate-field' => array(1)
+         ),
+       );
+     }
+ 
      if ($this->allow_advanced_render()) {
        $form['alter']['#tree'] = TRUE;
        $form['alter']['alter_text'] = array(
diff -r -p views/includes/query.inc views-patched/includes/query.inc
*** views/includes/query.inc	2009-06-02 19:17:06.000000000 +0100
--- views-patched/includes/query.inc	2009-07-22 08:28:02.000000000 +0100
*************** class views_query {
*** 935,940 ****
--- 935,956 ----
          $string = "COUNT($string)";
          $has_aggregate = TRUE;
        }
+       else if (!empty($field['sum'])) {
+         $string = "SUM($string)";
+         $has_aggregate = TRUE;
+       }
+       else if (!empty($field['average'])) {
+         $string = "AVG($string)";
+         $has_aggregate = TRUE;
+       }
+       else if (!empty($field['minimum'])) {
+         $string = "MIN($string)";
+         $has_aggregate = TRUE;
+       }
+       else if (!empty($field['maximum'])) {
+         $string = "MAX($string)";
+         $has_aggregate = TRUE;
+       }
        else if (!empty($field['aggregate'])) {
          $has_aggregate = TRUE;
        }
