Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
For each field which contains aggregation type, ex. COUNT, groupby key is added to mysql. However some times either this field doesn't have a corresponding database column (ex. generated by views calc) or this field is just not the group key, we might want to remove this field name from the group by list. Is there a way to do it ? thanks.
Comments
Comment #1
dawehnerIn theory all handlers can specify "usesGroupBy" and then get excluded based on that properties,
maybe the fields that use views calc(don't know much about that) could do that.
Comment #2
windmaomao CreditAttribution: windmaomao commentedok, thanks dawehner, that's interesting. I don't know you can exclude groupby.
this groupby is added by Views, i tracked it down to the following code in views_plugin_query_default.inc, I comment out one of the line which includes groupby for formula, i don't know if this will cause problem on other views yet. At least it solves the problem for now.
I wonder why groupby needs to be added for formula or non-aggregation fields ? puzzled.
Comment #3
merlinofchaos CreditAttribution: merlinofchaos commentedIn the handler for your field, add this code:
Then that field will not be given aggregation functions at all.
Comment #5
mpotter CreditAttribution: mpotter commentedSorry, I'm going to re-open this.
This feature request is still valid for other fields. For example, I have a complex aggregation query where I want to have certain fields available in the query, but I do *not* want them included in the GroupBy. But these are standard fields (such as Title) so adding use_group_by is not a valid solution. Isn't there a way to add an "Ignore" option to the list of Aggregation options?
Here is an example query I am trying to reproduce in Views:
In the above query, adding Title to the GROUP BY messes up the intended query results, but I still need it in the list of fields to return.
Comment #6
mpotter CreditAttribution: mpotter commentedHmm, nevermind. I just found this: http://drupal.org/node/1089694#comment-4357936 which explains that each field in the SELECT must either be in the GROUP BY or in an aggregation according to SQL standards and that MySQL is just violating this. I'll try putting something like MIN() around the Title and Nid fields to see if I can get that to work.
Comment #8
rlmumfordI'm going to reopen this because I still can't see anyway to remove a field completely from the aggregation stuff. I have written a variety of views field handlers containing complex logic including group functions. For example:
SUM((cost.num_days) * cost.cost_pd) / COUNT(DISTINCT fees_alias.retained)
and
IF(COUNT(DISTINCT fees_alias.retained) > 0, (SUM(fees_alias.fee) - SUM((cost.num_days) * cost.cost_pd) / COUNT(DISTINCT fees_alias.retained)), - SUM((cost.num_days) * cost.cost_pd))
I don't want any additional grouping functions to a apply to these as they already contain SUMs but they can't be grouped by because they contain functions like SUM.
Comment #9
mcmacerson CreditAttribution: mcmacerson as a volunteer commented@rlmumford thank you for re-opening.
@mpotter with MySQL your solution works to
I probably don't understand enough about it but I find it strange that using the most popular database for Drupal leaves us needing to go thru each Aggregation setting and set it to some arbitrary value such as Minimum just to get the GROUP BY to work properly. Something as simple as a NONE option, set by default so we don't have to go thru each field item one by one to turn off "Group results together" would be nice. At least it's working now but having to set each field's Aggregation setting to an arbitrary value seems like a kludge.
Comment #10
damir.gadiev CreditAttribution: damir.gadiev commentedHello!
I've had similar issue. Any formula, created in add_field would add this field to grouping, if any other field uses grouping.
I solved it in the next way.
1. First, in hook_views_query_alter() added my required field( existing field may be used as well)
2. Then, define
'query_aggregate'
plugin, name it CUSTOM_AGGREGATION_PLUGIN and do Your logic there. Field will not be added to group by section of query.Comment #11
Hamulus CreditAttribution: Hamulus commenteddamir.gadiev, could you please explain point 2 more detailed: how and where you defined query_aggregate