With a field from this module, the query includes a:
GROUP BY raw_sql_field
This breaks SUM() (and other calculated) aggregation, whether via the Views API or manually inserting it into the SQL.
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | fix_aggregation-1880612-2.patch | 668 bytes | DanZ |
Comments
Comment #1
sreynen commentedCan you maybe post your full query here? I'm not sure I understand what you're trying to do.
Comment #2
DanZ commentedThis is a view on the Ubercart uc_order_products table. I'm going for a SUM() on the custom SQL field. So....
Create the View on Ordered products.
Advanced settings, turn on "Use aggregation".
Add some filters.
Add field, the Raw SQL field. Parameter is (uc_order_products.price * uc_order_products.qty). Aggregation setting is SUM.
SQL generated is:
Note the raw_sql_field in the GROUP BY stuff. As long as it's grouping on that field, a SUM() will never work. Also note that it doesn't do SUM() in the SELECT for the raw_sql_field.
The order_product_qty field, a regular schema field DOES do it correctly. It has a SUM() in the SELECT and nothing in the GROUP BY.
I tried copying in stuff from the parent class query() method, and it seems to work. Try out this patch.
Comment #3
DanZ commentedComment #4
DanZ commentedIt's all numeric aggregation (not just SUM()), and it relates to the SQL that Views generates, not the SQL entered by the admin setting up a field.
Comment #5
sreynen commentedI haven't been able to reproduce this to test the patch. Can you recreate this problem on a simpler view (ideally a node view) and export the view?
Your instructions say you're applying SUM to the raw SQL field, but your query shows SUM on a different field. I tried both, and the raw SQL field isn't showing up in the GROUP BY at all for me.
Comment #6
DanZ commentedYes, there's no SUM() on the raw SQL field. There is a GROUP BY, instead, even though I set up the field with SUM aggregation. That's the bug I'm reporting.
Note that, pre-patch, the handler has no use of $this->options['group_type']. That's where it says to use SUM().
I'll see if I can get a simpler view to demonstrate this bug.
Comment #7
technikh commentedGROUP BY can be altered using http://drupal.org/project/views_extra_handlers
http://drupal.org/files/project-images/veh_group_by_0.png
Comment #8
DanZ commentedI haven't forgotten about this, and will get that sample view...eventually....
Comment #9
les limSorry, accidental post.
Comment #10
ruloweb commentedHi all,
I had the same issue, raw sql didnt play ok with aggregation, after the patch, it works.
Thanks so much!
Comment #11
Niremizov commentedThis is still actual. Have the same behaviour and yes - patch fixes the issue.
Use Aggregation: Enabled
Views Raw SQL: 1+1
Views Raw SQL aggregation option: SUM
Screenshot of view configuration and resulting SQL: https://yadi.sk/i/RQ81aIPVsRiA4
As result, no SUM() expression appears at all. After appliing patch SUM expression works as desired, result: https://yadi.sk/i/I9Cb-1gNsRiH9
Comment #12
geek-merlinThanks for the patch and testing!
Rebased and committed to dev.