First, I love views. Thank you for the main thing that makes my customizations easier.

I'd like to know if its possible to extend the existing view module by adding in support for non-Count aggregates. Specifically, I'd like to add in support for MAX and MIN so that I can create a list of Recent blog posts, from all registered users on the site, where I expect to return 1 blog post per user.

If I had could write a view that uses MAX I could simply return apply a filter or set of filters that return the maximum node id per user for a set of nodes with a type of blog post.

I think I understand how the views module is structured enough to write this myself. But I'd like to know if I'm about to duplicate work that others have done in the past.

A few key points:
1. You already have support for grouping.
2. You already have special cases for things like distinct
3. Supporting aggregate functions would be easier than the work already done to support distinct, as all I'd have to do is write a set of functions that puts something like "MAX(this)" into the fields array.

correct me if i'm wrong.

Comments

cosmicdreams’s picture

Here's what I'm thinking of :

added to views_node.inc...

...in the filter part of node_views_tables()...

'mostrecent' => array(
        'name' => t('Node: Most Recent Node from Users'),
        'handler' => 'views_handler_filter_mostrecent',
        'value-type' => 'array',
        'help' => t('BETA: This filter will return one node per user.  Use only if you to limit the output of nodes per user.'),
      ),

...as a new function in views_node.inc...

/*
 * Create a filter to return most recent node from each user
 */
function views_handler_filter_mostrecent($op, $filter, $filterinfo, &$query) {  
    $query->add_aggregate_max('node.nid');
    $query->add_groupby('node.uid');
}

...in views_query.inc

function add_aggregate_max($field) {
    $this->fields[] = "MAX($field)";
  }

Obviously there is a lot of work and bugs created by this change, but its a start

q_man’s picture

Status: Active » Needs review

Since you are including some code and prepared to do some work on this I have changed the status to patch (code needs review).

Hopefully this will mean that the project mods will be more keen to look at it and reply.

This is also a feature I would love to have in views since I would like to use it to build a custom image gallery with one image from each user in a list of users.

merlinofchaos’s picture

Version: 6.x-2.x-dev » 5.x-1.x-dev

This isn't 2.x =)

q_man’s picture

Hi merlinofchaos,

Good to see you stopped by this post.
Did you correct it to apply to 5.x-1.x-dev?

If so does your lack of other comments mean that because of this it is not a priority or that you have simply no interest in developing this functionality in Views?

This would solve so many setup problems for my clubs site we would be prepared to offer a bounty of $100USD for anyone who sucessfully creates a patch to achieve the above functionality which gets included in a future release of Views (Ideally 2.0 as we will be upgrading when Drupal 6 and views 2 final are released)

merlinofchaos’s picture

Sorry, cosmicdream; the lack of response did indicate that this is not high on my radar. I'm not closing this down, though, but I admit it's something I'm unlikely to put much work into on my own for now.

tommo’s picture

subscribing - hope to help later if i can

sun’s picture

Version: 5.x-1.x-dev » 6.x-2.x-dev
Status: Needs review » Active

Too much for 5.x. Dunno, whether this still makes sense for 6.x.

eriktoyra’s picture

Subscribing... this would be a very useful feature. I must have this to sort out the latest unmoderated revision of a node in a view using the Revision Moderation module. For that I need to use MAX(). GROUP BY will only give me the second latest revision for some reason...

lunaris’s picture

Hi guys,

I've attempted a patch for this problem that's attached to #396380: Enhance Views to support proper GROUP BY queries -- this seems to be the master issue for grouping and aggregation in views. It's not much yet, but hopefully this'll go somewhere.

lunaris

merlinofchaos’s picture

Status: Active » Closed (duplicate)

Marking dup, it's really just another group by need.