How to count or add all values for an integer field in Views

This may well qualify as a Drupal cheap trick. But it is pretty darn useful if you ask me. The use case - I have a custom content type for daycares. In it, daycare owners can advertize the number of available spaces they have at any time. Creating a list of daycares showing available spaces is, of course, easy with Views. But what if I would like to show the total number of available spaces for the listed daycares. How would I do that? At first, it's not obvious. But after the usual searching spree, I found the perfect solution. The good news is you can do it all with Views - don't need a special module for that. By the way, my custom daycare content type contains various fields including an integer field for Available spaces. Here we go.

  • Create a new field-based view sorted by Unsorted (i.e. NO SORT) to which you'll add a block display (no need for a page display here).
  • Add the Available spaces field. Remove all other fields.
  • In the Advanced section, check Use aggregation. Save.
  • Back in the FIELDS section, click Aggregation settings and set the type to SUM. Save. The field should now look like so: SUM(Content : Available spaces) | Aggregation settings.
  • Click SUM(Content : Available spaces), click on Rewrite results and check Rewrite the output of this field using the available replacement tokens.

E.g. Total number of available spaces: [field_daycare_available_spaces]. where [field_daycare_available_spaces] is the token available for the field that holds the number of available spaces in my content type. Of course, this token will change based on the name of the field that holds the values that you want to add.

  • Add the required filters.
  • Save you view.
  • Go to the blocks admin page and configure your block.
  • Sit back and enjoy.

Got to love those cheap tricks...

Submitted by synthetic (not verified) on Thu, 03/14/2013 - 11:40

Permalink

Good article, except Im left unsure where the results of SUM are printed within the block? Ie, when the sum function is run, where does it print the result?

Submitted by adse (not verified) on Thu, 08/15/2013 - 22:29

Permalink

Merci pour cet article et le moment de bonheur !

The SUM of available spaces for the listed daycares is calculated by Views and outputted in a 'Block' display. Views automatically creates a block named 'Views: [name of your display]'. As usual, you can find this block at admin/structure/block. Note that you can't preview the output in the blocks UI. For that, you need to go to the Views UI and click on 'Auto preview' at the bottom of the page.

Submitted by Justin (not verified) on Thu, 12/11/2014 - 00:33

In reply to by nia (not verified)

Permalink

This kind tutorial was EXACTLY what I needed and nothing worked, removed the sort and now works perfectly. Thanks nia and all.

Submitted by Fredj (not verified) on Sat, 04/26/2014 - 18:29

Permalink

Bonsoir,

merci cet article m'a vraiment aidé ! A noter qu'il faut supprimer les critères de tri sinon la vue affichera toujours une liste de champs même si l'aggrégation est activée (Views 3.7)

Hi rmr2. If it doesn't work at first, do not despair. Start over again one step at the time. Go slow, make sure. I've added a few more notes. Believe me, it works.

Submitted by jjj (not verified) on Wed, 02/12/2014 - 22:01

Permalink

Hi, thanks.... it works fine.... very much thank to the info...

Submitted by Amy (not verified) on Wed, 06/25/2014 - 15:50

Permalink

It didn't work until I remove the "sort" criteria. But once I did, it worked perfectly. Thanks!

Submitted by Dom (not verified) on Sat, 06/03/2017 - 15:57

Permalink

Merci pour cet article simple et sympa.
Ça fonctionne très bien pour moi

Submitted by Paul F. (not verified) on Sat, 10/18/2014 - 05:27

Permalink

Remove sort or it won't work! Damn Views.

Submitted by ren.admin (not verified) on Sat, 10/18/2014 - 10:51

Permalink

Thanks @Amy & Paul F. I've updated the first bullet.

Submitted by Stephen (not verified) on Mon, 01/30/2017 - 20:46

Permalink

I have a view page with several attachment views beneath it. I need to add totals for each attachment, but the field names used in each attachment come from different entityforms and have different names, so is there a way to "collect" the totals from each attachment section to get a page "overall total"? Would Views aggregation do this, or can that only sum the same field?

Hey Stephen, I'm not an aggregation expert but I don't think there's a right way to achieve what you want with Views aggregation. Perhaps you could try with views_php.module, although writing a custom Views plugin would be more appropriate. IMHO, your use case is over-stretching the original scope intended for Aggregation.