I have for a module I created, to maintain member data for a karate club, a custom views_handler_field with as query:

  function query() {
    $this->ensure_my_table();
    $this->add_additional_fields();
    $this->field_alias = $this->table . '_' . $this->field;
    $this->query->add_groupby($this->aliases['vid']);
    
    $join = new views_join();
    $join->construct('clubmember_belt', $this->table_alias, 'vid', 'vid');
    
    $this->belts_table = $this->query->ensure_table('clubmember_belt', $this->relationship, $join);
    $this->query->add_field(NULL, "MIN($this->belts_table.gid)", $this->field_alias);
  }

the reason for this query is to grab, from a related table, the entry where clubmember_belt.gid has the lowest number....

This creates, in views, the following query

SELECT node.nid AS nid,
   clubmember.vid AS clubmember_vid,
   MIN(clubmember_belt.gid) AS clubmember_first_belt
 FROM node node 
 LEFT JOIN clubmember clubmember ON node.vid = clubmember.vid
 LEFT JOIN clubmember_belt clubmember_belt ON clubmember.vid = clubmember_belt.vid
 WHERE (node.status <> 0) AND (node.type in ('clubmember'))
 GROUP BY clubmember_vid, nid, clubmember_first_belt

This query however has an error: Can't group on 'clubmember_first_belt'

This code used to work on an older version of Views2 but since going to the latest version it fails.

I see no way to remove the 'clubmember_first_belt' from the group by list. If I do this manually the query works just fine....

I did a rewrite by adding the clubmember_first_belt via a pre_render function and that works but then I can not get click_sort to work correct.

When I change the code in 'views_plugin_query_default.inc' I can get it to work correct. See patch.

CommentFileSizeAuthor
filename.patch802 bytesbwynants

Comments

merlinofchaos’s picture

Status: Needs review » Closed (won't fix)

You have to add the field with the aggregate argument set to true, then it will not add it to the group by. additional_fields() doesn't have a mechanism to do that, so you are going to need to add the field with add_field() instead.

bwynants’s picture

Status: Closed (won't fix) » Active

>so you are going to need to add the field with add_field()

isn't that what I was doing?

i changed the line add_field to (cfr views_handler_filter_search.inc)

$this->query->add_field('', "MIN($this->belts_table.gid)", $this->field_alias, array('aggregate' => TRUE));

but this does not help?

Can you please give some more info?

Thanks in advance.

dawehner’s picture

Status: Active » Needs work

This patch is for 3.x so you would have to redo you patch, because group by support was commited

bwynants’s picture

i'm at 3.0 but have no clue what I should be changing to my code. can you give a clue? is there an example?

dagmar’s picture

You can see the new handlers here: http://drupal.org/cvs?commit=287316

bwynants’s picture

How can this be working?

The file views_handler_argument_group_by_numeric.inc contains a class named views_handler_argument shouldn't this class be called views_handler_argument_group_by_numeric

dagmar’s picture

Title: add_groupby statement creates an illegal group by for a formula field » how to create a custom group_by field
Category: bug » support
Status: Needs work » Active

So, this is not a bug.

bwynants’s picture

Status: Active » Fixed

Ok

I changed my code to

  function query() {
    $this->ensure_my_table();
    $this->add_additional_fields();
    $this->field_alias = $this->table . '_' . $this->field;
    $this->query->add_groupby($this->aliases['vid']);
    
    $join = new views_join();
    $join->construct('clubmember_belt', $this->table_alias, 'vid', 'vid');
    
    $this->belts_table = $this->query->ensure_table('clubmember_belt', $this->relationship, $join);
    $this->query->add_field('', "MIN($this->belts_table.gid)", $this->field_alias, array('aggregate' => TRUE));
  }

And this seems to work fine now.

PS: The patch in this issue is irrelevant in 3.0!

merlinofchaos’s picture

Well, #6 is clearly a bug. Checking in a fix for that. =)

bwynants’s picture

thx, and thanks also for the help, #1 works fine with the latest 3.0 code

Status: Fixed » Closed (fixed)

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