I am trying to use views with the group by feature and it doesn’t seem to be working as it should.

In the simple case where I have one field which is used for group by I get the results and the query as expected.

However if I try to add a second field (integer) which I wish to use for aggregation (avg in my case but I the bug persists for each aggregation type). I then get a spurious set of results and a query with a lot of extra feilds in the group by clause.

The only other thing that could be causing it is that I am using a commerce product as the base type rather than a node.

Comments

bojanz’s picture

Using aggregate functions with Field API fields is not possible at the moment, it's still broken.

JeremyFrench’s picture

Thanks for info.

Is there an issue for that? Or can this one be used for tracking it?

bojanz’s picture

Version: 7.x-3.0-alpha1 » 7.x-3.x-dev

Can't remember one at the top of my head, so let's keep it here for now.

obrienmd’s picture

subscribe

kevinob11’s picture

I created a duplicate of this ticket, here is my use case for more information.

When I use group by in views with a field from fieldapi views appears to be including too many of the columns from the fieldapi table in the query. A description of the view I tested this with is below, along with the code from the view.

I am grouping by node type, then doing a count of nid and an average of a fieldapi field named field_small that is just an integer. If I get rid of field_small the grouping works perfect. When field_small is included the group by statement includes language, delta, bundle, entity type, and even value (which should not be included). In the select statement it is averaging the entity id, instead of averaging the value. The query is below, followed by what I would think the query should be.

------------current query
SELECT
node.type AS node_type,
field_data_field_small.entity_type AS field_data_field_small_entity_type,
field_data_field_small.delta AS field_data_field_small_delta,
field_data_field_small.language AS field_data_field_small_language,
field_data_field_small.bundle AS field_data_field_small_bundle,
field_data_field_small.field_small_value AS field_data_field_small_field_small_value,
COUNT(node.nid) AS nid,
AVG(field_data_field_small.entity_id) AS field_data_field_small_entity_id

FROM
{node} node
LEFT JOIN {field_data_field_small} field_data_field_small ON node.nid = field_data_field_small.entity_id AND (field_data_field_small.entity_type = :views_join_condition_0 AND field_data_field_small.deleted = :views_join_condition_1)

GROUP BY
node_type,
field_data_field_small_entity_type,
field_data_field_small_delta,
field_data_field_small_language,
field_data_field_small_bundle,
field_data_field_small_field_small_value

LIMIT 10 OFFSET 0

-----------what query should be

SELECT
node.type AS node_type,
COUNT(node.nid) AS nid,
AVG(field_data_field_small.field_small_value) AS field_data_field_small_field_small_value

FROM
{node} node
LEFT JOIN {field_data_field_small} field_data_field_small ON node.nid = field_data_field_small.entity_id AND (field_data_field_small.entity_type = :views_join_condition_0 AND field_data_field_small.deleted = :views_join_condition_1)

GROUP BY
node_type

LIMIT 10 OFFSET 0

Thanks,

Kevin

lennart’s picture

I can confirm this issue on field type 'Integer'.

Shadlington’s picture

Subbed

merlinofchaos’s picture

I made an attempt at getting group by support working for field api, but frankly I think we have a ways to go. Still, have a minimal configuration actually working.

kevinob11’s picture

@merlinofchaos Thanks! Very excited about this feature, I have very little dev experience but have a bit of SQL knowledge and would be happy to do some testing when the time comes.

merlinofchaos’s picture

Status: Active » Fixed

I'm going to mark this fixed; the basic feature works. The rest of the way to go is in this issue right now: http://drupal.org/node/1073350

lennart’s picture

To make it work I needed to remove the "sort criteria" from the particular view where I wanted grouping. Just a note if other people are trying. Thanks.

Status: Fixed » Closed (fixed)

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