Using aggregation in Views

Published: 10.12.2011
Categories: Technology
Reading time: 1 min
Developer with a laptop

This article explores the Views aggregation feature in Drupal, a powerful tool for grouping and summarizing data displayed through Views. It offers a simplified explanation for those who haven't grasped the concept yet.

Anyone who has been using Drupal more than just a little bit knows that Viewsopens-in-a-new-tab is one of the most important tools you got in the Drupal toolbox. Personally I’m convinced that Drupal wouldn’t be where it is if it wasn’t for Views. It just rocks.

With the 3.x branch, a number of new awesome features were added to Views – most notably the pluggable query backend, but also things like plugins for headers and footers, and/or groupings for filters and the ability to turn off node access query rewrites. But one of the additions did always escape my attempts to understand it: use aggregation. No matter how I tried to tweak the Views settings, it just didn’t make sense to me.

If you don’t want to watch screencasts, here’s short-short version (spoiler alert!):

  • Aggregation makes Views not return items from the database, but groups of items.
  • You find the aggregation setting in the advanced section in Views.
  • Add any fields you want to use for grouping, in aggregation mode “group similar results”.
  • Add any fields you want to do calculations on – sums, averages, counts, etc. (Set the appropriate aggregation mode.)
  • Make sure you don’t, for example, have sorting on publish date – it will make your aggregation more or less meaningless. (Views adds this by default, which was the thing stopping my attempts to understand aggregation.)

Credit to the Views maintainersopens-in-a-new-tab. If you feel that Views is a good thing, it can’t hurt to let the maintainers know.

Related content

Loading...