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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:17:38+00:00 2026-05-26T09:17:38+00:00

I am using postresql but i am realy bad at construting sql queries. I

  • 0

I am using postresql but i am realy bad at construting sql queries.
I have this query and it works:

SELECT handhistories FROM handhistories 
JOIN pokerhands using (pokerhand_id)  
JOIN gametypes using (gametype_id)
RIGHT OUTER JOIN playerhandscashkeycolumns using (pokerhand_id)
     WHERE pokerhands.site_id=0  
     AND pokerhands.numberofplayers>=5 and  pokerhands.numberofplayers<=7
     AND (bigblind = 2 OR bigblind = 4 )
     AND player_id in 
        (SELECT player_id FROM playerhandscashkeycolumns GROUP BY player_id
         HAVING AVG(case didvpip when true then 100::real else 0 end) <= 20 )

but i also want to limit the last “having” from the bottom so it will be something like this, but ofcourse it does not work.

SELECT handhistories FROM handhistories 
JOIN pokerhands using (pokerhand_id)  
JOIN gametypes using (gametype_id)
RIGHT OUTER JOIN playerhandscashkeycolumns using (pokerhand_id)
       WHERE pokerhands.site_id=0  
       AND pokerhands.numberofplayers>=5 and  pokerhands.numberofplayers<=7
       AND (bigblind = 2 OR bigblind = 4 )
       AND player_id in 
        (SELECT player_id FROM playerhandscashkeycolumns GROUP BY player_id
         HAVING AVG(case didvpip when true then 100::real else 0 end) <= 20
         AND  HAVING AVG(case didvpip when true then 100::real else 0 end) > 10 )

how to “save” value that is after the having so i can compare it also from the bottom?
Thank you all.

  • 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-26T09:17:39+00:00Added an answer on May 26, 2026 at 9:17 am

    This is mostly what @wildplasser already pointed out
    .. minus the mistake with BETWEEN
    .. plus JOIN instead of IN construct, which is usually faster in PostgreSQL.
    .. easier to read

    SELECT handhistories
    FROM   handhistories
    JOIN   pokerhands USING (pokerhand_id)  
    JOIN   gametypes USING (gametype_id)
    RIGHT  JOIN playerhandscashkeycolumns USING (pokerhand_id)
    JOIN   (
        SELECT player_id
        FROM   playerhandscashkeycolumns
        GROUP  BY player_id
        HAVING avg(CASE WHEN didvpip THEN 100::real ELSE 0 END) >  10
        AND    avg(CASE WHEN didvpip THEN 100::real ELSE 0 END) <= 20
        ) p USING (player_id)
    WHERE  pokerhands.site_id = 0  
    AND    pokerhands.numberofplayers BETWEEN 5 AND 7
    AND    bigblind IN (2,4);
    

    You table-qualify some columns, like pokerhands.site_id, but not others, like handhistories, you might want to clean that up.

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

Sidebar

Related Questions

I have just installed PostgreSQL 8.3.4 on Mac OS X 10.5 (using ports), but
Using PostgreSQL 8.4, I'm trying to put together the following query: SELECT (field_a +
I have found several topics with this title, but none of their solutions worked
Using PostgreSQL triggers, is it possible to record the changes that have happened to
Since I'm using Postgresql and can't use LINQ to SQL, I wrote my own
I am switching from using SQLite3 to PostgreSQL, and hoped that I could populate
How can i change database encoding for a PostgreSQL database using sql or phpPgAdmin?
I'm using Hibernate EntityManager, and am experiencing a weird slowdown in my Hibernate queries.
I want to know how to retrieve results in a SQL query doing some
I have a SQL function called get_forecast_history(integer,integer) that takes two arguments, a month and

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.