Add grouping support for views calc

sun - May 1, 2007 - 21:48
Project:Views Calc
Version:6.x-1.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:active
Description

It would be really nice if Views Calc was also able to group a view by a column and calculate a sum/average/count of another column.

Example:

  • Currently, Views Calc is able to calculate and display the sum of comments by user:
    user    nid    comments
    KarenS  1      11
    KarenS  2      9
    sun     1      4
    sun     2      8
    Sum     -      32
  • With additional grouping by user and removing the field nid in this view, Views Calc could calculate the sum of all comments in all nodes per user:
    user    comments
    KarenS  20
    sun     12
    Sum     32

Is this possible with Views Calc?

#1

ray007 - June 19, 2007 - 11:01

subscribing

#2

moshe weitzman - July 3, 2008 - 16:08
Version:5.x-1.x-dev» 6.x-1.x-dev

Since D6 Views has grouping, this is more likely to be implemented there (if at all).

#3

KarenS - July 5, 2008 - 11:14

Actually, Views won't do any totaling of the group values, so we still need something else. But hopefully the new grouping in Views 2 will make this easier to do.

#4

GregoryHeller - February 1, 2009 - 00:28

I noticed this too. If you select a field in the "Grouping Field" drop down in the "views calc style" settings in views2, the view will be grouped by the field you selected, and the views calc "total" line will show up at the bottom of each "grouping" of nodes, HOWEVER the total will always be the total of all rows! So if you have 5 groupings of a total of 25 nodes, and you are summing a column where the value of all the fields is "1" (thus the total sum is 25) this total will be shown below EACH grouping where one would expect to see a "sub total"

#5

cardentey - February 6, 2009 - 17:01

subscribing :-)

#6

PRZ - March 17, 2009 - 21:47

subscribing

#7

gunzip - March 30, 2009 - 12:48

subscribe. there's another module that does this anyway: http://drupal.org/project/views_groupby
but it's in early developement stage. maybe joining forces ?

#8

wundo - March 31, 2009 - 23:03
Title:Add grouping support for views» Add grouping support for views calc
Version:6.x-1.x-dev» 6.x-1.0

#9

punkassfender - April 8, 2009 - 23:53

subscribing

#10

visualnotion - April 10, 2009 - 16:43

I've been racking my brain to figure out how to get this functionality only to find out it may not be possible, right now. Any ideas of how to group and total like Sun requested without Views_Calc?

I've got a sports league site I'm trying to finish developing and I was hoping to do a write-up on how it was done (seems to come up every now and then without a decent solution to date).

Any help in getting this implemented would be appreciated.

#11

KarenS - April 21, 2009 - 12:19

The views_groupby field works totally differently than views_calc so I don't think they can be combined. Plus it only does counts.

We already have the grand totals and subtotals, so the remaining request would be an option to hide the source data and display only the totals, which should be fairly easy to add.

#12

KarenS - April 21, 2009 - 12:23

As to #4, we have both grand totals and sub totals for each page (a recent change to the code).

As to #10, there are no easy alternative ways to do that, that is the purpose of this module. I use Views Calc extensively for things like sports team totals (see http://www.everbloom.us/results for some examples).

#13

KarenS - April 21, 2009 - 12:53

Hmm... well there are actually a number of different things noted in this issue. I just added an option to surpress the details and show the totals only and it now shows both subtotals and grand totals. The one thing left is the original issue, which I mis-read or misunderstood, to be able to group by a field and then to calculations on that total. That is not yet implemented.

I'm going to push out a new release with a number of bug fixes. This feature will take more thought so it won't go in and will remain a feature request.

#14

Daniel A. Beilinson - April 21, 2009 - 21:20

Hi, KarenS!
I try to get sum of money cck fields. And when i try to add to my table field with few taxonomy terms i see few rows with one term in one row. If i use group multiple values option i see sum of all rows without grouping multiple values... Can you fix it?
P.S. In new version i choose only sum and module gives me Total COUNT, Total SUM, Total AVG, Total MIN, Total MAX. Can you fix it?

#15

KarenS - April 22, 2009 - 02:21

Please don't dump unrelated issues together, this is about a feature request for grouping. There is another issue about problems with multiple values and the project page says this is a known issue. The last thing is a bug that got introduced in the last commit, but it didn't belong in this issue either.

#16

introfini - May 8, 2009 - 22:50

subscribing...

#17

jrust - May 21, 2009 - 06:20

Subscribe

#18

klahaeck - June 4, 2009 - 23:35

Subscribe

#19

artedemingo - June 8, 2009 - 01:19

Subscribe

#20

Aren Cambre - June 11, 2009 - 17:07
Version:6.x-1.0» 6.x-1.x-dev

subscribe. This would be quite useful. If I turn on grouping, I get no calcs at all on certain fields.

#21

seabrawk - August 7, 2009 - 19:49

subscribe

#22

seabrawk - August 7, 2009 - 19:57

This isn't the same as displaying all the info on one page at the same time and so probably doesnt work for most of you, but you could create a grouping "taxonomy" for each thing you'd normally group, and and then expose that filter in your view as a selectable list...

#23

li8461 - August 11, 2009 - 00:49

subscribe

#24

JamieR - August 18, 2009 - 22:20

Subscribe

Also, incase anyone is interested, I solved my need to total a grouped field with a theme override function for my view... it ended up looking like this:

<div class="field-item">$<?php
 
/* Total all the grouped numbers. */
 
foreach ($field->field_values[$row->nid] as $value) {
   
$total += $value['value'];
  }
  print
$total;
?>
</div>

In a template file named views-view-field--field-charge-amount-value.tpl.php.
Hope that helps someone!
Jamie.

#25

JamieR - August 27, 2009 - 20:31

I had someone ask me to explain this in a little more detail, so here goes.

I'm using this technique to total a CCK field that allows "Unlimited" values. You can total it in a view created by the Views 2 module, or in the normal node view. I'll just show how to do the Views 2 one. But I highly recommend installing the Advanced Help module. After installed, you can look to the CCK help page for info on how to theme the node view (help/content/theme-field-templates).

The field that I wanted to total is called field_charge_amount.

  1. Go to you view edit page and click on the "Theme: Information" link in the lower left.
  2. You should now see a (LARGE) list of theme override templates below. Find the theme template name for your field... mine was called Charge Amount, so I found this list:

    Field Content: Charge Amount (field_charge_amount) (ID: field_charge_amount_value): views-view-field.tpl.php, views-view-field--field-charge-amount-value.tpl.php, views-view-field--ro-print-tag.tpl.php, views-view-field--ro-print-tag--field-charge-amount-value.tpl.php, views-view-field--default.tpl.php, views-view-field--default--field-charge-amount-value.tpl.php, views-view-field--ro-print-tag--default.tpl.php, views-view-field--ro-print-tag--default--field-charge-amount-value.tpl.php

    It shows the active template that is being used in bold.

  3. If we want to override it for all cases of the field in a view, we will want to add a template file called views-view-field--field-charge-amount-value.tpl.php to your theme (/sites/default/themes/your_theme/). You just pick the name of the template that fits the amount of specificity you want for the field. Anyway, at this point click on the Field name at the beginning of this block to get the template file contents. Mine looked like this:

    <?php
    // $Id: views-view-field.tpl.php,v 1.1 2008/05/16 22:22:32 merlinofchaos Exp $
    /**
      * This template is used to print a single field in a view. It is not
      * actually used in default Views, as this is registered as a theme
      * function which has better performance. For single overrides, the
      * template is perfectly okay.
      *
      * Variables available:
      * - $view: The view object
      * - $field: The field handler object that can process the input
      * - $row: The raw SQL result that can be used
      * - $output: The processed output that will normally be used.
      *
      * When fetching output from the $row, this construct should be used:
      * $data = $row->{$field->field_alias}
      *
      * The above will guarantee that you'll always get the correct data,
      * regardless of any changes in the aliasing that might happen if
      * the view is modified.
      */
    ?>

    <?php
    print $output;
    ?>

  4. Next step is to make the file with the default content pasted into it and put it in your theme folder. When that is done I'd update the theme registry and reload the theme info page on the view to see if the bolded theme has changed to yours. The views theme info screen has a button at the bottom you can click to update the theme registry.
  5. Now you are ready to edit your new theme template file. I found the variables that I needed in the $field object, and needed to cros reference the view id from the $row object. My favorite way to look inside variables is with this line: drupal_set_message("<pre>".print_r($row,1)."</pre>");. You should be able to see just the important variables like this: drupal_set_message("<pre>".print_r($field->field_values[$row->node_vid],1)."</pre>");
  6. So now that you can get to your values, you can do whatever you want with them. In my final version I decided to output each row and then the total at the end with a different class so that I could style in a little dividing line above it. Here is what mine looked like:
    <?php
     
    /* Total all the grouped numbers. */
     
    if (!empty($field->field_values[$row->{$field->field_alias}])) {
        foreach (
    $field->field_values[$row->{$field->field_alias}] as $value) {
         
    $total += $value['value'];
          print (!empty(
    $value['value'])) ? '<div class="field-item">$'.$value['value']."</div>\n" : null;
        }
        if (
    $total > $value['value']) {
          print
    '<div class="field-item-total">$'.$total."</div>\n";
        }
      } else {
        print
    '<div class="field-item">none</div>'."\n";
      }
    ?>
  7. Then your last step is just to fancy it up with some css as normal and you're done!
  8. Hope that helps someone. Theme overriding can do some amazing, super useful stuff. Totaling a field is just one. :)
    Let me know if you have any questions. Thanks! Jamie.

#26

seabrawk - August 26, 2009 - 01:45

First of all, thanks...

Secondly, I made it all the way to "updating the theme registry and reload the theme info page on the view to see if the bolded theme has changed to yours."

However, it never changed the bolded theme to mine, even after creating a txt file named (in my case) "views-view-field--field-stat-nonpass-td-value.tpl.php," which contained the default php text, and was placed in my "sites/default/modules/views/theme/" directory...

Lastly, From where does one run this command: "drupal_set_message("".print_r($row,1)."");"

Thanks again for all the help, and sorry if I am asking rudimentary stuff...

#27

JamieR - August 27, 2009 - 20:28

No problem. The theme template file that you just made would go in /sites/default/themes/your_theme_name/ ... not in the views module's theme folder.
Then to answer your second question, the set message goes into that theme template file that you just put in there... Make sense?

Let me know - good luck!
Jamie.

#28

doknet - September 2, 2009 - 21:16
Title:Add grouping support for views calc» RELATED QUESTION

I made a new post, with a matter related to this.
I want to get a view (with views_calc), with a table that adds a given number field giving separately the subtotal for each group of nodes.

http://drupal.org/node/566714

I desire obtain the sum of values in a given field, separating the subtotal for each content type grouping they belong.

I appreciate the cooperation of the members of the forum.

Sorry for my English

Thanks

#29

Aren Cambre - September 3, 2009 - 00:47
Title:RELATED QUESTION» Add grouping support for views calc

Please start a new issue for new questions.

#30

akolahi - September 8, 2009 - 02:02

I'm not sure if this is helpful, but perhaps to help prevent reinventing the wheel, it may be worth mentioning that the Charts module (http://drupal.org/project/charts) is able to perform the following operations from views groupings: sum, count, average, minimum and maximum. The user provides the grouping field, the operation, the field to perform the operation and the field to aggregate the results on. Works well with views.

Perhaps the code may be helpful in getting similar functionality into views calc. Just a thought.

#31

doknet - September 8, 2009 - 18:39

Thanks

#32

seabrawk - October 13, 2009 - 22:08

Finally...

its a views patch, so obviously its not a supported thing, but if you NEED it:

http://drupal.org/node/396380

The fix in #29 did exactly what I needed...

 
 

Drupal is a registered trademark of Dries Buytaert.