Problem/Motivation
When using the regular "group_by" (views aggregation) option to calculate a sum (say of a numeric field), the views aggregator plus column sum function returns the sum of the first n (depending on the views result count) entity fields instead of the sum of the aggregated sums.
Steps to reproduce
Create a view of entities with a numeric field and a second field for grouping (in my case a taxonomy), set views aggregation (not the table aggregation!) to calculate the sum of the numeric field grouped by the other field. Set views aggregator plus table and set column operation to sum. Behold the wrong sum.
Proposed resolution
In getCellRaw, try to access $result_row values directly before doing additional steps.
| Comment | File | Size | Author |
|---|---|---|---|
| #7 | trouble-calculating-column-sum-on-views-group_by-3241057-7.patch | 1.44 KB | tvoesenek |
Issue fork views_aggregator-3241057
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
attisanComment #3
jordik commentedIs there a particular reason for you to use the Views Aggregator Plus in combination with the native views aggregation?
All the functionality you describe is covered by Views Aggregator Plus - just use Group Aggregation.
On the column you want to group by set Group and compress on the other columns use Sum. Use Sum in Column Aggregation for the total sums.
Comment #4
rex.barkdoll commentedI'm not the OP, but here's my example:
I'm working with Drupal Commerce's Daily Sales Report View.
In my brain, when I started all of this, (and this is how my comment here relates to the original post) I just wanted to keep the original settings from Views Aggregation and then turn on the column Sum function and have it show me the total number of orders placed. If I couldn't do that, I felt like I figured out how to recreate the table using all of Views Aggregator Plus' functions, but it still failed to give me the result I want.
For the developers, I think part of the problem you're fighting is that something that feels like it should be intuitive wasn't. This might be solved with examples and documentation.
The other part is that, in my case, the patch above hasn't solved my problem and I'm not sure how else to get this calculation to run correctly.
Just to be succinct: My issue is that I have a column that is using the count function to get a total number of sales for the day based on how many order IDs there are, but the column's sum function isn't using the result of the count that's visually displayed, it's just adding all the ID's together, resulting in a number totally different than what the count function is returning.
Let me know if I need to put my problem in a new issue. Happy to do that.
Comment #5
jordik commentedIf you use count in the column aggregation it should give you the correct number (17).
Comment #6
marioki commentedI second the need for this feature, because it greatly improves performance if grouping is done by the database. Patch #2 works well, is there any reason not to include this in the module, at least as a configurable option so existing views do not break?
Comment #7
tvoesenek commentedThis is a reroll of patch #2 against 2.1.x
Comment #8
tr commentedAlso, see #3322452: Warning: Undefined array key XXX in Drupal\views_aggregator\Plugin\views\style\Table->getCellRaw() which makes a very similar change for a different reason.