In function query in views_plugin_query_default.inc

    $this->has_aggregate = FALSE;
    $non_aggregates = array();

    list($non_aggregates) = $this->compile_fields($fields_array, $query);

    if (count($this->having)) {
      $this->has_aggregate = TRUE;
    }
    if ($this->has_aggregate && (!empty($this->groupby) || !empty($non_aggregates))) {
      $groupby = array_unique(array_merge($this->groupby, $non_aggregates));
      foreach ($groupby as $field) {
        $query->groupBy($field);
      }
      if (!empty($this->having) && $condition = $this->build_condition('having')) {
        $query->havingCondition($condition);
      }
    }

I'm trying to use add_groupby but couldn't get it to work until I added a having, to make it through this code. Why shouldn't I be allowed to have a SQL "GROUP BY" withoug a "HAVING"?

Comments

dawehner’s picture

I'm not really sure what's your point ... there is a !empty($this->having), so only if you have having it gets added.

Your problem is probably that you have configured your conditions to met a count criteria?

jody lynn’s picture

But $this->has_aggregate will always be FALSE unless you have something in $this->having. So my group by does not work unless I add a having.

merlinofchaos’s picture

Unless there's some other way to set has_aggregate, Jody Lynn is totally right. And I don't know the answer to her question. This seems to be an error?

dawehner’s picture

Category: support » bug

Oh i see...

Well true, this seems to be an error, do you want to provide a patch? I think just moving out the condition would be fine here.

jody lynn’s picture

Yeah, I'm not clear on the intention of $this->has_aggregate, what it's for and where else it is used. In this section of code it seems useless though.

VivienLetang’s picture

I confirm the bug. A simple way to resolve it is to remove the test on $this->has_aggregate

I will build a well formatted patch tommorow morning

rgristroph’s picture

I can confirm that I am also not able to do a add_groupby() without also doing a add_having(). Also, when I do the add_groupby() on one field I get in the SQL a "GROUP BY" on all the fields, but I think that is a separate bug.

ronino’s picture

Status: Active » Needs review
StatusFileSize
new598 bytes

But $this->has_aggregate will always be FALSE unless you have something in $this->having. So my group by does not work unless I add a having.

$this->has_aggregate is also used and set in views_plugin_query_default::compile_fields(), but I don't really understand how to trigger that. I helped myself by adding $this->query->add_having_expression(0, '1') in my handler which finally adds the required GROUP BY.

In case the test
if ($this->has_aggregate && (!empty($this->groupby) || !empty($non_aggregates))) {
is indeed a bug, I added a patch to remove the has_aggregate condition.

Status: Needs review » Needs work

The last submitted patch, views-has_aggregate-1512488-8.patch, failed testing.

ronino’s picture

Status: Needs work » Needs review
StatusFileSize
new616 bytes

Updated patch to comment #8.

liam mcdermott’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new673 bytes

We've just been doing some work on User Stats and have a View that's using GROUP BY, this had us banging our heads against a brick wall!

Anyway, here's a re-rolled patch, I've tested and it works as expected.

dawehner’s picture

Issue tags: +Needs tests

Stuff like that should really really have a test, i'm sorry but these are critical code paths, which should be backed up :(

liam mcdermott’s picture

Stuff like that should really really have a test

Sorry, I don't follow. This needs tests written, or I should mark the status 'needs review' so the patch will be automatically tested?

dawehner’s picture

Status: Reviewed & tested by the community » Needs work

If it's a bug, you can write a simpletest, in order to be sure that your change fixes the issue.
Set to needs work, in order to write a test for it. Sorry I think these changes in the central pieces should be avoided as much as possible.

alcroito’s picture

Issue summary: View changes
Status: Needs work » Closed (duplicate)