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 8131925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:13:08+00:00 2026-06-06T09:13:08+00:00

I had the following query that doesn’t work and I was wondering how to

  • 0

I had the following query that doesn’t work and I was wondering how to fix it.

Player.select("players.*, 
    (SELECT COUNT(*) FROM Results 
    WHERE results.player_id = players.id and win = true) 
as wins").where("wins > 0").order("wins desc")

I’m trying to limit parent Player records by a count of how many times its foreign key appears in the Results table when the boolean win is set to true. However, sometimes the foreign key will appear in the Results table but the boolean win field will be false resulting in a count of zero for records that I don’t want to see, so I thought I would try and eliminate these count of zero records using the .where(“wins > 0”) clause but I get this error:

PGError: ERROR: column “wins” does not exist

The funny thing is it finds the wins field when I try and order by it, but not with the added where clause.

  • 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-06T09:13:09+00:00Added an answer on June 6, 2026 at 9:13 am

    I believe the following query will work:

    select players.*, count(results.id) as wins from players left join results on results.player_id = players.id and results.win = true group by players.id having count(results.id) > 0 order by wins desc;
    

    This should Railsify thus, though naturally I haven’t tested it:

    Player.select('players.*, count(results.id) as wins').joins('left join results on results.player_id = players.id and results.win = true').group('players.id').having('count(results.id) > 0').order('wins desc')
    

    If you don’t care about rows where wins = 0, you can do an inner join instead of a left join, which might be faster.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query that looks like the following: SELECT * FROM table WHERE
I have the following query (MySQL): SELECT col1, col2 FROM database1.table ->WHERE col3 !=
I'm trying to preform a SELECT query that grabs data from two tables only
I had the following template that essentially changed the color of the border of
I had the following problem today, and I was wondering if there is a
I'm building a collection of RESTful resources that work like the following: (I'll use
The following query is pretty simple. It selects the last 20 records from a
I have a gridview that is bound to the result from an nhibernate query.
I have a SQL query that is returning a table like the following: id
IQueryable<double?> query = (from t in ctx.MyList orderby t.MyNums select t.MyNums).Take(1); IQueryable<double> q2 =

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.