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...

Comments

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?

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.

make sure that you remove the sort for the aggregation to work.

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

It just outputs the same list of values without adding them together.

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.

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

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

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

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

Add new comment

Plain text

  • Lines and paragraphs break automatically.
  • Allowed HTML tags: <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
Refresh Type the characters you see in this picture.
Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.