robertgarrigos's picture

I have an aggregated view created with the views_aggregator contrib module, which has an average column. I would like to order the view by this aggregated value. Is that possible? Right now, it doesn't seem to work. This NOTA column is an aggregated value and is the field used in the sort section of the view, but it is not sorted:

 

Comments

robertgarrigos's picture

Aha, although it gets ordered when using the configurations of the view format.

robertgarrigos's picture

wow, the order set in the view's format settings only works with php 8.1+, not with php 7.4.... 

Hi Robert. I'm looking at D7 issues about this - apparently this is possible since 10 years ago, which means the functionality is there, but there may be some issues with the way your view is configured. Could you take a picture of the Settings UI within the Format option of Views? Id like to see how you are grouping and compressing by club and then calculating the average to the other column (both in "group aggregation").

Hmm... I was able to reproduce this issue when I thought it would work. Wuold you mind opening an issue in the queue?

BTW, this is still a problem in the D7 version (despite what that issue said), so I'm not sure this is possible. I'll have to look more closely into it.

I'm reposting AND expanding my answer here:

As you noticed, enabling column sorting in Format > Settings works, but selecting a sort in the "Sort Criteria" UI does not.

So, the temporary solution to this is to not use "Sort Criteria" at all, and instead use the Format > settings to sort by the average column. If you don't want to allow the user to click on other columns, just do not click on any of the "Sortable" checkboxes. Only click on the radio button for the average column. 

 

ALTERNATIVELY (a better solution) is to not use Views Aggregator Plus at all, and instead turn on aggregation in the View itself. The benefit of doing this is that the grouping and average calculation is done in the SQL query, rather than in pre-rendering (Views Aggregator's approach). The latter approach can be very resource intensive for large sets of data, as it's highly inefficient. Obviously the Aggregate setting of Views cannot be used in all instances, but I believe it could be used in your example:

  1. Make the View a "Table" (not a "Table with aggregation options")
  2. Under Other, turn on the Use aggregation (this will add "Aggregation settings" to all fields, which can be very confusing. Just ignore that)
  3. In the grouping field (club), do not change any setting
  4. In the average field ("Notas"), click Aggregation settings and select "Average"
  5. Do not change settings for any filter fields or any other fields
  6. Add "Notas" as in the Sort Criteria region, click "Aggregation settings" and also select Average 

That's it. Then your results will be aggregated and sorted correctly. I believe you may need to put your grouping field above the average in the list of fields.