Index: theme.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/views_calc/theme.inc,v
retrieving revision 1.11
diff -u -p -r1.11 theme.inc
--- theme.inc	16 Jan 2011 16:49:02 -0000	1.11
+++ theme.inc	24 Jan 2011 01:39:40 -0000
@@ -191,46 +191,61 @@ function views_calc_table_total(&$vars, 
   $fields   = &$view->field;
   $columns  = $handler->sanitize_columns($options['columns'], $fields);
 
-    $vars[$key] = array();
-    $added_label = array();
+  $vars[$key] = array();
+  $added_label = array();
 
-    foreach ($view->views_calc_fields as $calc => $calc_fields) {
-      $row = $totals[$calc];
-      foreach ($columns as $field => $column) {
-        $field_alias = $fields[$field]->field_alias;
-        if ($field == $column && empty($fields[$field]->options['exclude'])) {
-          if (in_array($field, $calc_fields)
-            && isset($row->$field_alias)) {
-            // COUNT is always a numeric value, no matter what kind of field it is.
-            if ($calc == 'COUNT') {
-              $vars[$key][$calc][$column] =  $row->$field_alias;
-            }
-            // Calculations other than COUNT should run the value through the field's theme.
-            // This will allow dates and numeric values to apply the right formatting to the result.
-            else {
-              $vars[$key][$calc][$column] =  $fields[$field]->theme($row);
-            }
-          
+  // Build a data baserow.
+  $baserow = new StdClass();
+  foreach ($view->field as $field) {
+    $query_alias = $field->field_alias;
+    $baserow->{$query_alias} = $totals->{$query_alias};
+  }
+  // Build aggregation rows, one per function.
+  foreach ($view->views_calc_fields as $calc => $calc_fields) {
+    // Rebuild row as if it where single queries.
+    $row = drupal_clone($baserow);
+    foreach ($view->field as $field) {
+      $query_alias = $field->field_alias;
+      $ext_alias = $calc.'__'.$query_alias;
+      if (in_array($field->field, $calc_fields)) {
+        $row->{$query_alias} = $totals->{$ext_alias};
+      }
+    }
+    // Build row output data.
+    foreach ($columns as $field => $column) {
+      $field_alias = $fields[$field]->field_alias;
+      if ($field == $column && empty($fields[$field]->options['exclude'])) {
+        if (in_array($field, $calc_fields)
+          && isset($row->$field_alias)) {
+          // COUNT is always a numeric value, no matter what kind of field it is.
+          if ($calc == 'COUNT') {
+            $vars[$key][$calc][$column] =  $row->$field_alias;
           }
+          // Calculations other than COUNT should run the value through the field's theme.
+          // This will allow dates and numeric values to apply the right formatting to the result.
           else {
-            // Add the calc type label into the first empty column.
-            // Identify which is the sub total and which the grand total
-            // when both are provided.
-            if (empty($added_label[$calc])) {
-              if ($key == 'sub_totals') {
-                $label = t("Page !Calculation", array("!Calculation" => $calc));
-              }
-              else {
-                $label = t("Total !Calculation", array("!Calculation" => $calc));
-              }
-              $vars[$key][$calc][$column] = $label;
-              $added_label[$calc] = TRUE;
+            $vars[$key][$calc][$column] =  $fields[$field]->theme($row);
+          }
+        }
+        else {
+          // Add the calc type label into the first empty column.
+          // Identify which is the sub total and which the grand total
+          // when both are provided.
+          if (empty($added_label[$calc])) {
+            if ($key == 'sub_totals') {
+              $label = t("Page !Calculation", array("!Calculation" => $calc));
             }
             else {
-              $vars[$key][$calc][$column] = '';
+              $label = t("Total !Calculation", array("!Calculation" => $calc));
             }
+            $vars[$key][$calc][$column] = $label;
+            $added_label[$calc] = TRUE;
+          }
+          else {
+            $vars[$key][$calc][$column] = '';
           }
         }
       }
     }
+  }
 }
Index: views_calc_table.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/views_calc/views_calc_table.inc,v
retrieving revision 1.24
diff -u -p -r1.24 views_calc_table.inc
--- views_calc_table.inc	23 Jan 2011 13:22:03 -0000	1.24
+++ views_calc_table.inc	24 Jan 2011 01:39:40 -0000
@@ -97,7 +97,8 @@ class views_calc_table extends views_plu
     }
     $this->view->totals = array();
     $this->view->sub_totals = array();
-    $this->view->views_calc_fields = $this->get_calc_fields();
+    $this->view->views_calc_fields = $calc_fields;
+    $this->view->views_calc_calculation = FALSE;
       
     // Subtotals and pager totals require a list of the specific
     // values to include.
@@ -114,18 +115,14 @@ class views_calc_table extends views_plu
       // We need one query per aggregation because theming needs unrenamed views field alias.
       // TODO Looks like we have problems unless we
       // force a non-page display, need to keep an eye on this.
-      foreach ($calc_fields as $calc => $field) {
-        $this->execute_summary_view($calc, $ids);
-      }
+      $this->execute_summary_view($ids);
     }
     
     // Add grand totals to the results.
-    foreach ($calc_fields as $calc => $field) {
-      $this->execute_summary_view($calc);
-    }
+    $this->execute_summary_view();
   }
 
-  function execute_summary_view($calc, $ids = array()) {
+  function execute_summary_view($ids = array()) {
     // Clone view for local subquery.
     $summary_view = $this->view->clone_view();
 
@@ -152,7 +149,7 @@ class views_calc_table extends views_plu
     $is_subtotal = !empty($ids);
     $summary_view->preview = TRUE;
     $summary_view->is_cacheable = FALSE;
-    $summary_view->views_calc_calculation = $calc;
+    $summary_view->views_calc_calculation = TRUE;
     $summary_view->views_calc_sub_total = $is_subtotal;
     $summary_view->views_calc_ids = $ids;
     $summary_view->views_calc_fields = $this->view->views_calc_fields;
@@ -172,10 +169,10 @@ class views_calc_table extends views_plu
     $summary_view->post_execute();
     if (!empty($summary_view->result)) {
       if ($is_subtotal) {
-        $this->view->sub_totals[$calc] = array_shift($summary_view->result);
+        $this->view->sub_totals = array_shift($summary_view->result);
       }
       else {
-        $this->view->totals[$calc] = array_shift($summary_view->result);
+        $this->view->totals = array_shift($summary_view->result);
       }
     }
   }
@@ -185,13 +182,12 @@ class views_calc_table extends views_plu
    */
   function query() {
     parent::query();
-    
+
     // If we're not getting a summary row, do nothing.
     if (empty($this->view->views_calc_calculation)) {
       return;
     }
     // If there are no calc fields, do nothing.
-    //if (!$calc_fields = $this->get_calc_fields()) {
     if (!$this->view->views_calc_fields) {
       return;
     }
@@ -209,8 +205,6 @@ class views_calc_table extends views_plu
    */
   function query_total() {
     // Create summary rows.
-    $calc = $this->view->views_calc_calculation;
-    $fields = $this->view->views_calc_fields[$calc];
     
     // Empty out any fields that have been added to the query,
     // we don't need them for the summary totals.
@@ -220,21 +214,24 @@ class views_calc_table extends views_plu
     $this->view->query->orderby = array();
     $this->view->query->groupby = array();
 
-    foreach ($this->view->field as $field) {
-      $query_field = substr($field->field, 0, 3) == 'cid' ? $field->definition['calc'] : $field->table .'.'. $field->field;
-      $query_alias = $field->field_alias;
-      // Bail if we have a broken handler.
-      if ($query_alias == 'unknown') {
-        continue;
-      }
-      $this->view->query->add_table($field->table, NULL, NULL, $field->table);
-      if (!empty($fields) && in_array($field->field, $fields)) {
-        // Calculated fields.
-        $this->view->query->add_field(NULL, "$calc($query_field)", $query_alias);
-      }
-      else {
-        // Empty fields that have no calculations.
+    $calc_fields = $this->view->views_calc_fields;
+    foreach ($calc_fields as $calc => $fields) {
+      foreach ($this->view->field as $field) {
+        $query_field = substr($field->field, 0, 3) == 'cid' ? $field->definition['calc'] : $field->table .'.'. $field->field;
+        $query_alias = $field->field_alias;
+        // Bail if we have a broken handler.
+        if ($query_alias == 'unknown') {
+          continue;
+        }
+        $this->view->query->add_table($field->table, NULL, NULL, $field->table);
+        // add all fields
         $this->view->query->add_field(NULL, "NULL", $query_alias);
+        // aggregation functions
+        $ext_alias = $calc.'__'.$query_alias;
+        if (in_array($field->field, $fields)) {
+          // Calculated fields.
+          $this->view->query->add_field(NULL, $calc . '(' . $query_field . ')', $ext_alias);
+        }
       }
     }
     // TODO This won't work right with relationships, need a fix here.
