Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

count with group by discards :distinct option

#5721

A simple count does generate the correct SQL :

Post.count(:user_id, :distinct => true)
# SELECT COUNT(DISTINCT "posts"."user_id") AS "count_user_id" FROM "posts"

But mixing group and count discards the :distinct option:

Post.group(:date).count(:user_id, :distinct => true)
# SELECT COUNT("posts"."user_id") AS "count_user_id", date AS date
# FROM "posts" GROUP BY date

Reported by Julien Portalier · September 28th, 2010 @ 10:10 AM

State: resolved
Milestone: 3.0.2
Assigned to: Aaron Patterson Aaron Patterson
Importance: Low

Activity

  1. Neeraj Singh
    Neeraj Singh
    • State changed from new to open
    • Importance changed from to Low

    September 28th, 2010 @ 09:12 PM

  2. Neeraj Singh
    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' ?

    September 28th, 2010 @ 09:17 PM

  3. Julien Portalier
    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.

    September 29th, 2010 @ 11:54 AM

  4. Marcelo Giorgi
    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.

    October 1st, 2010 @ 12:40 PM

  5. Andrea Campi
    Andrea Campi
    • Tag changed from activerecord calculation, patched to activerecord calculation, patch

    bulk tags cleanup

    October 11th, 2010 @ 07:21 AM

  6. Santiago Pastorino
    Santiago Pastorino
    • Milestone cleared.
    • Assigned user changed from Santiago Pastorino to Aaron Patterson

    October 11th, 2010 @ 01:48 PM

  7. Repository
    Repository
    • State changed from open to resolved

    October 11th, 2010 @ 05:59 PM

  8. Repository
  9. Jeremy Kemper
  10. Ryan Bigg
    Ryan Bigg
    • Tag changed from activerecord calculation, sheepskin boots, patch to activerecord calculation patch

    Automatic cleanup of spam.

    October 16th, 2010 @ 02:22 AM