Question

I'm using Drupal's Views 2, and need to retrieve min values from fields in a custom table. The query for this is easy if I were writing it by hand--something like, "SELECT foo, min(bar) FROM table GROUP BY foo." But how would I do this using Views? I've already defined the table in a views.info file, so there's no trouble getting views to see the table. It's the Min() part of the query I just don't understand.

My next stop will be the Views API documentation, but if someone can just provide the outline for how to do this quickly, I would greatly appreciate it.

No correct solution

OTHER TIPS

New answer to an old question, but something like this will work. You need to create a custom field handler and then wrap the field as follows:

class views_handler_custom_field extends views_handler_field {

  function query() {
    $this->ensure_my_table();
    $this->field_alias = $this->query->add_field("MAX({$this->table_alias}", "{$this->real_field})",$this->table_alias . "_" . $this->real_field);
  }
}
  1. Use aggregation from advanced configuration of views. After this is set yes you can select max, min or any other selector to fields.

Test your results but it should work well

  1. Alternatively in some cases you can sort your data ascending or descending and then just pick one to be shown on the view. Can be problematic when displaying multiple fields or so.

After testing first one seems to be faster at least on small scale.

One could consider the modules groupby and views_calc, but I assume they are not acceptable for you.
Additionally, you can accomplish this with a custom module.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow