3

I'm trying to work with aggregation in Views and I just can not seem to understand how to get it to do what it is supposed to be doing. First off, a simple example. I create a view with "Show" set to a parent table. (a list of accounts in this case). Next I add a relationship to a child table (a list of account entries). I add one field, the account name.

Finally I turn on aggregation and set the account name to "group by". The SQL statement it provides has no group by clause! The View says it is:

SELECT ledger_account.name AS ledger_account_name
FROM
{ledger_account} ledger_account
INNER JOIN {ledger_account_entry} ledger_account_entry_ledger_account 
    ON ledger_account.aid = ledger_account_entry_ledger_account.aid

Where I would have expected

SELECT ledger_account.name AS ledger_account_name
FROM
{ledger_account} ledger_account
INNER JOIN {ledger_account_entry} ledger_account_entry_ledger_account 
    ON ledger_account.aid = ledger_account_entry_ledger_account.aid
GROUP BY ledger_account_name

I know this is a simple example, but it's a piece to a larger summation with a few joins. However, if views isn't smart enough to do this correctly, or if I just fail to understand how to do this properly, my query will never work.

2

For a short tutorial about the "aggregation" features of the Views module, refer to Using aggregation in Views. As an alternative, have a look at the (free) set of 30 video tutorials about the Views module. Specific to "Aggregation", you may want to look at these episodes (quotes are from the linked pages):

  • The aggregation setting.

    ... makes Views return not one row per item in the database, but one row per aggregation group. Perhaps more importantly, it also makes it possible to count the number of items baked into each row, or sum them up, or calculate their average, or count the number of unique items, or…

  • Aggregation and relationships.

    ... shows how to use the aggregation setting in Views in combination with relationships.

  • Aggregation edge cases.

    ... shows some of the more rare use cases of aggregations in Views:

    • Using aggregation on filtering
    • Using aggregation on sorting
    • Using aggregation on contextual filters (which is currently broken)
    • NOT aggregation on relationships (since aggregation doesn't apply to this handler)
    • Using aggregation on multiple-value fields
  • Agreed. Aggregation is not quite what a db admin might expect in all cases. Thanks very much for these specific and descriptive links to these great video tutorials: I expected that I could group on a relationship entity_id so that if a given entity reference has multiple values, the parent record would not repeat itself. No dice so far... :( – texas-bronius Nov 16 '18 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.