views:

93

answers:

2

I need to count how many people belong in pre-defined groups (this is easy to do in SQL using the SELECT COUNT statement). My Views query runs fine and displays the actual data in my table, but I simply need to know how many results it found.

However there doesn't seem to be a COUNT option in views. I am guessing I am going to have to use some sort of views hook, and then stick the result in the table.

Here's a quick example of what i'm trying to achieve:

My Table
----------------------
Group A | 20 people
Group B | 63 people

and so on.

(I've tried using the Views_Calc module, but I get errors because it is not quite stable yet.)

Anybody know of an easy way to count results in Views?

+1  A: 

Here's a good d.o thread about it: http://drupal.org/node/131031

Although if you JUST need the count and not the other things Views offers (field formatting & ordering, etc), why not just code up the proper SELECT COUNT statement and call it a day?

(If you DO in fact need those other pieces Views offers, there are many examples on that thread above.)

Entendu
a lot of the posts are about placing the code in the footer. I need the count to be displayed in the table row. or did i miss something?
Jukebox
A: 

I'm currently using the Views Group By module for this type of functionality.

I'm actually working on adding other aggregate functions (MIN, MAX, etc.) into it but since you only need the COUNT function, I think it's a pretty good option.

All you have to do (after installing and enabling the module), in the View that you are interested:

  1. Add fields for the criteria that you want to GROUP BY.
  2. Add the SQL Aggregation field as the last field (or swap it into the last).
  3. Choose the fields (you can select multiple fields) Fields to Group On.
  4. SQL Aggregation Function should be set to Count.
  5. Fields to Aggregate with the SQL function set to a field that you are not grouping by. (This will be added into the COUNT function like COUNT(<this field>) in SQL)
  6. The rest is up to you and click Update.

You should have the COUNT output from the field that you selected to aggregate.

sirhc
I've been trying out this module ... unfortunately I've only been able to get to display "uid: 1" for every person in the group (instead of an actual count or total). I am grouping on "group", aggregating with "uid" and sorting with "group". any ideas?
Jukebox
That's odd. If you are getting every person in the group, then I don't think you are grouping successfully. You might want to check the output SQL. Also, check that you are not sorting by a criteria (in the Sorting Criteria block) that is not in the group by.
sirhc