This project is archived and is in readonly mode.
count with group by discards :distinct option
-
Neeraj Singh
- State changed from new to open
- Importance changed from to Low
-
Neeraj Singh
I am not fully sure what value distint will bring in this case since grouping is always by same vlaue.
In this case I have three cars with names: honda, toyota and toyota.
ree-1.8.7-2010.02 > Car.group(:name).count(:name, :distinct => true) SQL (0.3ms) SELECT COUNT("r_cars"."name") AS count_name, name AS name FROM "r_cars" GROUP BY name => #<OrderedHash {"honda"=>1, "toyota"=>2}>By distinct you mean the count for toyota should be '1' ?
-
Julien Portalier
Here is an example: I have posts published by users, and I want to know how many different users are publishing posts everyday.
Post.create(:user_id => 1, :created_on => '2010-09-29') Post.create(:user_id => 1, :created_on => '2010-09-29') Post.create(:user_id => 2, :created_on => '2010-09-29') Post.create(:user_id => null, :created_on => '2010-09-29') Post.group(:created_on).count) # => {'2010-09-29' => 4} Post.group(:created_on).count(:user_id) # => {'2010-09-29' => 3} Post.group(:created_on).count(:user_id, :distinct => true) # => {'2010-09-29' => 2}From the MySQL documentation:
COUNT(*)counts the total number of rows.COUNT(column)counts rows with non null column.COUNT(DISTINCT column)counts rows with different, non null column.
-
Marcelo Giorgi
- Assigned user set to Santiago Pastorino
- Tag set to activerecord calculation, patched
Hi,
I implemented a test and patch for this.
This applies cleanly to master now.
-
Andrea Campi
- Tag changed from activerecord calculation, patched to activerecord calculation, patch
bulk tags cleanup
-
Santiago Pastorino
- Milestone cleared.
- Assigned user changed from Santiago Pastorino to Aaron Patterson
-
Repository
- State changed from open to resolved
(from [dba7de0da0af6732c30484e8e16f22614cfc13f2]) Honor distinct option when used with count operation after group clause [#5721 count with group by discards :distinct option state:resolved] http://github.com/rails/rails/commit/dba7de0da0af6732c30484e8e16f22...
-
Repository
(from [1c9022de212c190362d40b98624dcf71d20ca073]) Honor distinct option when used with count operation after group clause [#5721 count with group by discards :distinct option state:resolved] http://github.com/rails/rails/commit/1c9022de212c190362d40b98624dcf...
-
Ryan Bigg
- Tag changed from activerecord calculation, sheepskin boots, patch to activerecord calculation patch
Automatic cleanup of spam.
