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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:03:07+00:00 2026-05-25T12:03:07+00:00

Having trouble with SQL (currently using postgresql) I have this query as I need

  • 0

Having trouble with SQL (currently using postgresql)

I have this query as I need to compare the most recent item and the second most recent item:

SELECT p1.*, p2.price_cents FROM "prices" p1 
  INNER JOIN 
    (
      SELECT price_cents, game_id from prices as p WHERE p.game_id = p1.game_id 
        ORDER BY p.created_at DESC LIMIT 1 OFFSET 1
    )
  p2 ON p2.game_id = p1.game_id 

This produces a few errors:

ERROR:  invalid reference to FROM-clause entry for table "p1"
LINE 1: ...AND p.game_id = p1.game_id...
                           ^
HINT:  There is an entry for table "p1", but it cannot be referenced from this part of the query.

Is there any reason I can’t access p1 from that subselect, is it a existence issue, as in, p1’s data isn’t available yet? Is there another way to do this with a JOIN?

  • 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-25T12:03:08+00:00Added an answer on May 25, 2026 at 12:03 pm

    Try this one

    SELECT p1.*, (
        SELECT price_cents 
        FROM "prices" p 
        WHERE p1.game_id = p.game_id  
        ORDER BY p.created_at DESC LIMIT 1 OFFSET 1
    ) as price_cents 
    FROM "prices" p1 
    

    UPDATE according to authors comment

    If you need more than one column from second recent entry, you can try following snippet

    SELECT * FROM (
        SELECT p.*, (
            SELECT id 
            FROM "prices" 
            WHERE p.game_id = game_id  
            ORDER BY created_at DESC LIMIT 1 OFFSET 1
        ) AS second_id 
        FROM "prices" p
    ) p1 INNER JOIN "prices" p2 ON p1.second_id = p2.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble with the following SQL statement. I have had this issue
I'm having trouble with a LINQ to SQL query getting the min value using
I'm having trouble creating a query. I need to create a SQL query where
I'm having some trouble with an SQL statement that have to find the number
I'm having trouble with a query in SQL Server 2008 on some forex trading
I'm having trouble trying to optimize the following query for sql server 2005. Does
I'm having trouble wrapping my head around more advanced Rails query methods. In this
Hi im having trouble with this sql. Basically it displays statuses that my friends
I'm having a bit of trouble figuring this one out. I have a dropdownlist
Having Trouble making sql connection. this is my code private static SqlConnection GetConnection() {

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.