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.

CommentFileSizeAuthor
#2 fix_aggregation-1880612-2.patch668 bytesDanZ

Comments

sreynen’s picture

Status: Active » Postponed (maintainer needs more info)

Can you maybe post your full query here? I'm not sure I understand what you're trying to do.

DanZ’s picture

Version: 7.x-1.0-rc1 » 7.x-1.x-dev
StatusFileSize
new668 bytes

This 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:

SELECT node_uc_order_products__field_data_field_supplier.field_supplier_value AS node_uc_order_products__field_data_field_supplier_field_supp, MIN(node_uc_order_products.nid) AS node_uc_order_products_nid, 'node' AS field_data_field_supplier_node_entity_type, (uc_order_products.price * uc_order_products.qty) AS raw_sql_field, SUM(uc_order_products.qty) AS uc_order_products_qty
FROM 
{uc_order_products} uc_order_products
LEFT JOIN {uc_orders} uc_orders ON uc_order_products.order_id = uc_orders.order_id
LEFT JOIN {users} users_uc_orders ON uc_orders.uid = users_uc_orders.uid
LEFT JOIN {node} node_uc_order_products ON uc_order_products.nid = node_uc_order_products.nid
INNER JOIN {users_roles} users_uc_orders__users_roles ON users_uc_orders.uid = users_uc_orders__users_roles.uid
LEFT JOIN {node} node ON uc_order_products.nid = node.nid
LEFT JOIN {field_data_field_supplier} node_uc_order_products__field_data_field_supplier ON node_uc_order_products.nid = node_uc_order_products__field_data_field_supplier.entity_id AND (node_uc_order_products__field_data_field_supplier.entity_type = 'node' AND node_uc_order_products__field_data_field_supplier.deleted = '0')
WHERE (( (uc_orders.order_status IN  ('payment_received', 'completed')) AND (users_uc_orders__users_roles.rid = '4') AND (node.type IN  ('book')) ))
GROUP BY field_data_field_supplier_node_entity_type, node_uc_order_products__field_data_field_supplier_field_supp, raw_sql_field
LIMIT 10 OFFSET 0

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.

DanZ’s picture

Status: Postponed (maintainer needs more info) » Needs review
DanZ’s picture

Title: SUM() aggregation fails » Views aggregation fails

It'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.

sreynen’s picture

Status: Needs review » Postponed (maintainer needs more info)

I 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.

DanZ’s picture

Your instructions say you're applying SUM to the raw SQL field, but your query shows SUM on a different field.

Yes, 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.

DanZ’s picture

I haven't forgotten about this, and will get that sample view...eventually....

les lim’s picture

Issue summary: View changes

Sorry, accidental post.

ruloweb’s picture

Hi all,

I had the same issue, raw sql didnt play ok with aggregation, after the patch, it works.

Thanks so much!

Niremizov’s picture

Priority: Normal » Critical
Status: Postponed (maintainer needs more info) » Reviewed & tested by the community

This 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

geek-merlin’s picture

Priority: Critical » Normal
Status: Reviewed & tested by the community » Fixed

Thanks for the patch and testing!

Rebased and committed to dev.

  • axel.rutz committed c39c1a4 on 7.x-1.x authored by DanZ
    Issue #1880612 by DanZ: Views aggregation fails
    

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.