Ruby-on-rails 同时计数和分组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/499767/
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
Counting and grouping at the same time
提问by DFectuoso
I have a Mailmodel with the following schema:
我有一个Mail具有以下架构的模型:
t.string "mail"
t.integer "country"
t.boolean "validated"
t.datetime "created_at"
t.datetime "updated_at"
And I want to find the top 5 countries in the database, so i go ahead and type
我想在数据库中找到前 5 个国家,所以我继续输入
@top5 = Mail.find(:all,:group => 'country',:conditions => [ "validated = ?" , "t" ], :limit => 5 )
This will tell me the groups(i need an order by i dont know how to write)
这将告诉我组(我需要一个我不知道怎么写的订单)
@top5 = Mail.count(:all,:group => 'country',:conditions => [ "validated = ?" , "t" ], :limit => 5 )
This will tell me how many mails are in each group
这将告诉我每个组中有多少邮件
Im wondering if i can group and count in just one go
我想知道我是否可以一次性分组和计数
回答by Can Berk Güder
Try:
尝试:
Mail.count(:group => 'country', :conditions => ['validated = ?', 't'])
Mail.count(:group => 'country', :conditions => ['validated = ?', 't'])
I'm not sure count accepts :limitthough.
我不确定 count 是否接受:limit。
EDIT:
编辑:
I think this is more readable:
我认为这更具可读性:
Mail.count(:group => :country, :conditions => {:validated => true})
Mail.count(:group => :country, :conditions => {:validated => true})
回答by tee
With Rails 3 you can simplify it further:
使用 Rails 3,您可以进一步简化它:
Mail.where(validated: true).count(group: :country)
You can order by fields in the group - in this case only :country would be valid:
您可以按组中的字段排序 - 在这种情况下,只有 :country 有效:
Mail.where(validated: true)
.order(:country)
.count(group: :country)
You can also order by the count, using "count_all":
您还可以使用“count_all”按计数排序:
Mail.where(validated: true)
.order("count_all desc")
.count(group: :country)
You can also limit the number of groups returned. To do this you must call limit before calling count (because #countreturns ActiveSupport::OrderedHash):
您还可以限制返回的组数。为此,您必须在调用 count 之前调用 limit (因为#count返回ActiveSupport::OrderedHash):
Mail.where(validated: true)
.order("count_all desc")
.limit(5)
.count(group: :country)
Updated syntax for Rails 4:
Rails 4 的更新语法:
Mail.where(validated: true)
.group(:country)
.count
回答by airportyh
Mail.find(
:all,
:select => 'count(*) count, country',
:group => 'country',
:conditions => ['validated = ?', 't' ],
:order => 'count DESC',
:limit => 5)
This should give you records that have a country attribute and a count attribute.
这应该为您提供具有国家属性和计数属性的记录。
回答by Rahul Sagore
I too had to group data with city nameand showing countof how many rows are there for each city with specific condition. So this is how I did:
我也必须用城市名称对数据进行分组,并显示每个具有特定条件的城市的行数。所以这就是我所做的:
CityData.where(:status => 1).group(:city_name).count
Output of this was:
这个的输出是:
{:Mumbai => 10, :Dublin => 7, :SF => 9}

