How to group by in Views 2
hook_views_pre_execute(&$view)
This hook is called right before the execute process. The query is now fully built, but it has not yet been run through db_rewrite_sql. Adding output to the view can be accomplished by placing text on $view->attachment_before
and $view->attachment_after
. So I'm using this to rewrite the SQL and add the GROUP BY which is missing in the views.
Solution : Implement this hook in your module and override it. Here how I did it
<?php
/**
* Implements hook_views_pre_execute().
*/
function MODULE_NAME_views_pre_execute(&$view) {
if ($view->name == 'VIEW_NAME' && ($view->current_display == 'CAN BE/BLOCK/PAGE/DEFAULT')) {
$count = 0;
$search = array('ORDER BY');
$replace = array('GROUP BY GROUP BY node_data_field_membership_type_field_membership_type_value ORDER BY');
$view->build_info['query'] = str_replace($search,
$replace,
$view->build_info['query'], $count);
// if there are no any order by then add the GROUP BY
if ($count == 0) {
$view->build_info['query'] .= ' GROUP BY node_data_field_membership_type_field_membership_type_value';
$view->build_info['count_query'] .= ' GROUP BY node_data_field_membership_type_field_membership_type_value';
}
else
$view->build_info['count_query'] = str_replace($search,
$replace,
$view->build_info['count_query']);
}
}
?>
© Heshan Wanigasooriya.RSS🍪 This site does not track you.