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 |
Jump to:
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
subscribing
#2
Since D6 Views has grouping, this is more likely to be implemented there (if at all).
#3
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
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
subscribing :-)
#6
subscribing
#7
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
#9
subscribing
#10
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
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
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
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
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
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
subscribing...
#17
Subscribe
#18
Subscribe
#19
Subscribe
#20
subscribe. This would be quite useful. If I turn on grouping, I get no calcs at all on certain fields.
#21
subscribe
#22
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
subscribe
#24
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
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.It shows the active template that is being used in bold.
views-view-field--field-charge-amount-value.tpl.phpto 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: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>");<?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";
}
?>
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
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
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
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
Please start a new issue for new questions.
#30
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
Thanks
#32
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...