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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:53:40+00:00 2026-06-04T05:53:40+00:00

I am trying to write a query to return the id of the latest

  • 0

I am trying to write a query to return the id of the latest version of a market index stored in a database.

SELECT miv.market_index_id market_index_id from ref_market_index_version miv 
INNER JOIN ref_market_index mi ON miv.market_index_id = mi.id 
WHERE mi.short_name='dow30' 
AND miv.version_num = (SELECT MAX(m1.version_num) FROM ref_market_index_version m1 INNER JOIN ref_market_index m2 ON m1.market_index_id = m2.id )

The above SQL statement can be (roughly) translated into the form:

SELECT some columns FROM SOME CRITERIA MATCHED TABLES 
WHERE mi.short_name='some name'
AND miv.version_num = SOME NUMBER

What I don’t understand is that when I supply an actual number (instead of a sub query), the SQL statement works – also, when I test the SUB query used to determine the latest version number, that also works – however, when I attempt to use the result returned by sub query in the outer (parent?) query, it returns 0 rows – what am I doing wrong here?

Incidentally, I also tried an IN CLAUSE instead of the strict equality match i.e.

... AND miv.version_num IN (SUB QUERY)

That also resulted in 0 rows, although as before, when running the parent query with a hard coded version number, I get 1 row returned (as expected).

BTW I am using postgeresql, but I prefer the solution to be db agnostic.

  • 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-04T05:53:41+00:00Added an answer on June 4, 2026 at 5:53 am

    The problem is probably that the max(version_num) doesn’t exist for ‘dow30’.

    Try the following correlated subquery:

    SELECT miv.market_index_id market_index_id
    from ref_market_index_version miv  INNER JOIN
         ref_market_index mi
         ON miv.market_index_id = mi.id 
    WHERE mi.short_name='dow30' AND
          miv.version_num = (SELECT MAX(m1.version_num)
                             FROM ref_market_index_version m1 INNER JOIN
                                  ref_market_index m2
                                  ON m1.market_index_id = m2.id
                             where m1.short_name = 'dow30'
                            ) 
    

    I added the where clause in the subquery.

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

Sidebar

Related Questions

I'm trying to write a query that will return the closest match from a
I'm trying to write a query to pull some info from our database and
I am trying to write a query that would select data from multiple lines
I'm trying to write a query in php that will select a row from
I am trying to write a linq query that will only return certain columns
I am trying to write a SQL query that returns rows from a table
I'm trying to write a HQL/Criteria/Native SQL query that will return all Employees that
I'm trying to write a query that will return all QUERY_ID values alongside all
I'm trying to write a query that will return data grouped by date ranges
I am trying to write a Facebook query that will return all comments posted

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.