Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3979480
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:10:31+00:00 2026-05-20T05:10:31+00:00

How can I count the number of results returned by a group query without

  • 0

How can I count the number of results returned by a “group” query without getting the data ? So far, I am just getting a hashtable of results. Is it possible in rails3 to optimize this query ?

Vote.group("question_id, user_id").where("question_id = 3").count.count
 => 2

In this case we are doing a count of this hashtable => {1=>10, 15=>1}

Query is:

SELECT COUNT(*) AS count_all, question_id, user_id AS question_id_user_id
FROM `votes`
WHERE (question_id = 3)
GROUP BY question_id, user_id
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:10:32+00:00Added an answer on May 20, 2026 at 5:10 am

    You can use count_by_sql:

    Vote.count_by_sql("select count(*) from ( select 1 from Votes group by question_id, user_id )")
    

    Or, you can build up the query using Rails, and then run it:

     query = Vote.group(:question_id, :user_id).to_sql
     count = Vote.count_by_sql("select count(*) from ( #{query} )")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How can I count the numbers of rows that a mysql query
I have a table and can count the number of rows using var count
I would like to know how I can count the number of unique values
How can I count the number of occurrences in a C string of /
How can I count the number of files in a directory using C on
Is there a way I can programmatically count the number of links for a
I can not seem to figure out how to count the number of seconds
In Rails 3.1, how can I get a count of the number of attributes
Can I assign 0 value on an array that returned no results from a
how can I make this sql query return zero for the count function instead

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.