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

  • SEARCH
  • Home
  • 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 7800957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:48:07+00:00 2026-06-02T00:48:07+00:00

I found some topics here on SO, but I still can’t find the right

  • 0

I found some topics here on SO, but I still can’t find the right setup for my query.

This is query, that works me well on localhost:

@cars = Car.find_by_sql('SELECT cars.*, COUNT(cars.id) AS counter 
                         FROM cars 
                         LEFT JOIN users ON cars.id=users.car_id 
                         GROUP BY cars.id ORDER BY counter DESC')

But on Heroku gives me the error above – GROUP BY clause or be used in an aggregate function.

Then I have read somewhere, that I should specify all columns in the table, so I tried this:

@cars = Car.find_by_sql('SELECT cars.id, cars.name, cars.created_at, 
                                cars.updated_at, COUNT(cars.id) AS counter 
                         FROM cars 
                         LEFT JOIN users ON cars.id=users.car_id 
                         GROUP BY (cars.id, cars.name, cars.created_at, cars.updated_at) 
                         ORDER BY counter DESC')

But this doesn’t work on localhost and also not on Heroku…

What should be the right config of the query?

  • 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-06-02T00:48:09+00:00Added an answer on June 2, 2026 at 12:48 am

    I think you are trying to aggregate and group by on the same column. It depends on what data you want. Ether do this:

    SELECT 
     cars.name, 
     cars.created_at, 
     cars.updated_at, 
     COUNT(cars.id) AS counter 
    FROM cars 
    LEFT JOIN users 
      ON cars.id=users.car_id 
    GROUP BY cars.name, cars.created_at, cars.updated_at 
    ORDER BY counter DESC
    

    Or you want to count all maybe? Then like this:

    SELECT
     cars.id,
     cars.name, 
     cars.created_at, 
     cars.updated_at, 
     COUNT(*) AS counter 
    FROM cars 
    LEFT JOIN users 
      ON cars.id=users.car_id 
    GROUP BY cars.id, cars.name, cars.created_at, cars.updated_at 
    ORDER BY counter DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've found some questions here about this topic, but none of the solutions solve
I don't understand this at all. Here is some Javascript code that works in
I read some topics here, but I haven't found the solution of my problem.
I found some mapkit code on the internet that looks like this: - (void)recenterMap
I found a bunch of other questions about this topic, but for some reason
I have been reading so many topics here about this, but I could not
I saw some other similar questions on this topic here but they were not
I have searched and found some info on this topic but the answers are
I've found information about this feature on SO some time ago, but the topic
I have tried to follow some suggestions found here in other similar topics, trying

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.