简体   繁体   English

计算视图中的结果数

[英]Count number of results in a View

I need to count how many people belong in pre-defined groups (this is easy to do in SQL using the SELECT COUNT statement). 我需要计算有多少人属于预定义的组(使用SELECT COUNT语句在SQL中很容易做到)。 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. 但是,视图中似乎没有COUNT选项。 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.) (我尝试过使用Views_Calc模块,但是我收到了错误,因为它还不太稳定。)

Anybody know of an easy way to count results in Views? 有人知道在Views中计算结果的简单方法吗?

Here's a good do thread about it: http://drupal.org/node/131031 这是关于它的好主题: 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? 虽然如果你只需要计数而不是View提供的其他东西(字段格式和排序等),为什么不只是编写正确的SELECT COUNT语句并将其称为一天?

(If you DO in fact need those other pieces Views offers, there are many examples on that thread above.) (如果你确实需要View提供的其他部分,那么上面的那个帖子就有很多例子。)

I'm currently using the Views Group By module for this type of functionality. 我目前正在使用Views Group By模块来实现此类功能。

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. 我实际上正在努力添加其他聚合函数( MINMAX等),但由于你只需要COUNT函数,我认为这是一个非常好的选择。

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 . 添加要用于GROUP BY的条件的字段。
  2. Add the SQL Aggregation field as the last field (or swap it into the last). 将SQL聚合字段添加为最后一个字段(或将其交换到最后一个字段)。
  3. Choose the fields (you can select multiple fields) Fields to Group On . 选择字段(您可以选择多个字段) Fields to Group On
  4. SQL Aggregation Function should be set to Count . SQL Aggregation Function应设置为Count
  5. Fields to Aggregate with the SQL function set to a field that you are not grouping by. Aggregate with the SQL function设置为您未分组的字段的Aggregate with the SQL function字段。 (This will be added into the COUNT function like COUNT(<this field>) in SQL) (这将被添加到COUNT函数中,如SQL中的COUNT(<this field>)
  6. The rest is up to you and click Update . 其余部分取决于您并单击“ Update

You should have the COUNT output from the field that you selected to aggregate. 您应该从您选择要聚合的字段中输出COUNT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM