Ruby-on-rails 如何在rails中按行计算分组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1647910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:53:58  来源:igfitidea点击:

How to count group by rows in rails?

ruby-on-railsrubygroup-bycount

提问by user199403

When I use User.count(:all, :group => "name"), I get multiple rows, but it's not what I want. What I want is the count of the rows. How can I get it?

当我使用时User.count(:all, :group => "name"),我得到多行,但这不是我想要的。我想要的是行数。我怎么才能得到它?

回答by nothing-special-here

Currently (18.03.2014- Rails 4.0.3) this is correct syntax:

目前(18.03.2014- Rails 4.0.3)这是正确的语法:

Model.group("field_name").count

It returns hash with counts as values e.g.

它返回带有计数作为值的哈希,例如

SurveyReport.find(30).reports.group("status").count

#=> {
  "pdf_generated" => 56
}

回答by hgmnz

  1. User.countwill give you the total number of users and translates to the following SQL: SELECT count(*) AS count_all FROM "users"
  2. User.count(:all, :group => 'name')will give you the list of unique names, along with their counts, and translates to this SQL: SELECT count(*) AS count_all, name AS name FROM "users" GROUP BY name
  1. User.count将为您提供用户总数并转换为以下 SQL: SELECT count(*) AS count_all FROM "users"
  2. User.count(:all, :group => 'name')将为您提供唯一名称列表及其计数,并转换为以下 SQL: SELECT count(*) AS count_all, name AS name FROM "users" GROUP BY name

I suspect you want option 1 above, but I'm not clear on what exactly you want/need.

我怀疑你想要上面的选项 1,但我不清楚你到底想要/需要什么。

回答by sikachu

Probably you want to count the distinct name of the user?

可能您想计算用户的不同名称?

User.count(:name, :distinct => true)

would return 3 if you have user with name John, John, Jane, Joey (for example) in the database.

如果数据库中有名为 John、John、Jane、Joey(例如)的用户,则将返回 3。

 ________
| name   |
|--------|
| John   |
| John   |
| Jane   |
| Joey   |
|________|

回答by sikachu

Try using User.find(:all, :group => "name").count

尝试使用 User.find(:all, :group => "name").count

Good luck!

祝你好运!

回答by CTS_AE

I found an odd way that seems to work. To count the rows returned from the grouping counts.

我发现了一种似乎有效的奇怪方法。对分组计数返回的行进行计数。

User Table Example

用户表示例

________
| name   |
|--------|
| Bob    |
| Bob    |
| Joe    |
| Susan  |
|________|

Counts in the Groups

分组中的计数


User.group(:name).count

# SELECT COUNT(*) AS count_all
# FROM "users"
# GROUP BY "users"."name"

=> {
  "Bob" => 2,
  "Joe" => 1,
  "Susan" => 1
}

Row Count from the Counts in the Groups

从组中的计数中计算行数

User.group(:name).count.count

=> 5

Something Hacky

一些黑客

Here's something interesting I ran into, but it's quite hacky as it will add the count to every row, and doesn't play too well in active record land. I don't remember if I was able to get this into an Arel / ActiveRecord query.

这是我遇到的一些有趣的事情,但它非常笨拙,因为它会将计数添加到每一行,并且在活动记录领域表现不佳。我不记得我是否能够将其放入 Arel/ActiveRecord 查询中。

SELECT COUNT(*) OVER() AS count, COUNT(*) AS count_all
FROM "users"
GROUP BY "users"."name"
[
  { count: 3, count_all: 2, name: "Bob" },
  { count: 3, count_all: 1, name: "Joe" },
  { count: 3, count_all: 1, name: "Susan" }
]