Using aggregation in Views
This blogpost is a celebration of the awesome aggregation functionality in Views 3. And that I finally understood how to use them.
Anyone who has been using Drupal more than just a little bit knows that Views is one of the most important tools you got in the Drupal tool box. 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.
Until a few days ago, that is. Now I know, thanks to patience from Island Usurper and emmajane. I am celebrating this by releasing three screencasts introducing aggregation, showing how to make use of aggregation and relationships, and using aggregation with other handlers or multiple-value fields.
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.)
PS 1: Adding these screencasts to the old Taming the Beast screencast series, I also add a "Cred to the Views maintainers" link. If you feel that Views is a good thing, it can't hurt to let the maintainers know.
PS 2: Don't miss the Rules Mastery training at DrupalCon Denver. It will rock.