Group by users.name

Events happening in the community are now at Drupal community events on www.drupal.org.
mp's picture

hello

D6, views2

to explain my problem will take some time, so be patient ;)

i have a taxonomy and it´s about fish ---> genus, species, subspecies, population.
on one site i want people to search after fishkeepers, so i made exposed filters where you can enter one or all of the terms (genus, species, subspecies, population).
if you view the site (without searching) then you get the exposed filters and below all fishkeepers. the problem is, if someone has 2 fish, then he is listed 2 times ... 10 fish - his name is 10x in the list

a guy, who i know wrote a code to remove double entries:
$users = array();
$usernames = array();
echo count($rows).'
';
foreach ($rows as $key => $row) {
if (!in_array($row['name'], $usernames)) {
$users[$key] = $row;
$usernames[] = $row['name'];
}
}
unset($usernames);

this works and you see every fishkeeper only one time, BUT :)

if you set in views, that you want 25 users / page then you get the wrong amount users per page, because views get a "wrong number". the user is listed 1 time, if he has 10 fish, then it counts 10 and this is what the "pageviewer" gets

the solution is to add the mySQL command "Group by users.name", but there is no way to set this in views.

can anyone help? i hope you understand my bad english :)

best wishes
markus

Comments

Bump...

DeeZone's picture

Just leaving a comment in order to track this thread as I too am interested in how to inject additional custom SQL into a View entry.

When you edit your View in

Garrett Albright's picture

When you edit your View in the GUI, look in the Basic Settings area and try setting the Distinct value to Yes. That might be what you're looking for.

Distinct is set to YES

mp's picture

distinct is set to yes, doesn´t work
it is set in both displays - default and page

Your last comment about

dwees's picture

Your last comment about "there is no way to do this in views" is actually incorrect. You can use hook_views_query_alter to add a GROUP BY clause before the query is run in a custom module.

  1. Implement hook_views_query_alter in your module (do a Google search for the function arguments).
  2. print out some debugging information about the $query variable.
  3. Use #2 to inform you as to how to add your "GROUP BY users.name" clause.

I've done something similar to this before.

Dave

Bingo!

DeeZone's picture

Thanks dwees, I knew the wisdom was out there somewhere :)

little problem since

mp's picture

little problem since views-update to 2.3

i had a little script running for groupbyuser

<?php

function groupbyuser_views_query_alter(&$view, &$query) {
  // add groupby
  if ($view->name == 'myview') {
    $query->groupby[] = 'users_name';
  }
}

and after the update the script doesn´t work - does anyone know why?

greets

Maybe the alias for the user

dwees's picture

Maybe the alias for the user name field changed somehow? Try debugging the view and see if that's what happened.

Dave

hi how can i debug this?

mp's picture

hi

how can i debug this? i´m a screendesigner and not a coder, i got this script from a coder

greets

Try using

dwees's picture

Try using this:

<?php

function groupbyuser_views_query_alter(&$view, &$query) {
 
// add groupby
 
if ($view->name == 'myview') {
   
drupal_set_message('<pre>' . print_r($query, TRUE) . '</pre>');
   
$query->groupby[] = 'users_name';
  }
}

?>

This should spit out the entire query in a reasonably formatted way, which you can try and analyze yourself, or post back here.

Dave

hi thanks, this works

mp's picture

hi

thanks, this works great
you can see the output at http://drupalbin.com/8360
users_name still exists ... and i saw, that the groupby array is empty

greets

Yeah if you reorder the two

dwees's picture

Yeah if you reorder the two lines inside the if() then statement, the group_by array should have your new addition. Not sure why it won't work then, seems like a bug to me? Try searching the issue queue for Views 2.3 and if you don't see your problem, describe it in detail there. I'm out of ideas.

Dave

hi ok, thanks for your

Behaviour of adding GROUP BY changed since 2.3?

pfaocle's picture

See this commit

Looks like its no longer possible to arbitrarily add single group by clauses like this?

Views Developers

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: