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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:41:13+00:00 2026-05-20T04:41:13+00:00

In my table I have two fields among others : article_id and version Example:

  • 0

In my table I have two fields among others : article_id and version

Example:

   article_id | version
   -----------|----------   
        5     |   1
        5     |   2
        6     |   1

What I want to do is to retrieve the latest version for each article id. (In my example I want to retrieve the article 5 version 2 object and article 6 and version 1 object).

The problem is that mysql is doing the group by instead of the order by so it returns to me the FIRST version of each article, but I want the opposite.

Do you have an idea please ?

Solution

select *
from article r
where r.version=(
 select max(version) 
 from article r2 
 where r2.article_id = r.article_id
);
  • 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-20T04:41:14+00:00Added an answer on May 20, 2026 at 4:41 am

    Your question is a bit vague, but I believe is is more or less this what you want:

    select * from (
       select
          <table>.*,
          row_number() over (partition by article_id order by version desc) r  
       from 
          <table>
    )
    where r = 1
    

    The query returns one record for each (distinct) article_id. This record is the one with the highest version for the article_id returned.

    So, together with a “test case”, this can be seen in action:

    create table tq84_resorces (
      id           number primary key,
      article_id   number not null,
      version      number not null
    );
    
    insert into tq84_resorces values (50, 5, 1);
    insert into tq84_resorces values (60, 5, 2);
    insert into tq84_resorces values (70, 6, 1);
    
    
    select * from (
       select
          tq84_resorces.*,
          row_number() over (partition by article_id order by version desc) r  
       from 
          tq84_resorces
    )
    where r = 1
    

    which returns:

            ID ARTICLE_ID    VERSION          R
    ---------- ---------- ---------- ----------
            60          5          2          1
            70          6          1          1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that consists of, among other things, two fields named StartTime
I have two fields in a table. One contains values such as BTA, BEA,
I have two tables with exactly the same fields. Table A contains 7160 records
Suppose I have a database table with two fields, foo and bar. Neither of
If I have a table with two foreign key fields to another table, I.E.
I have a query which searches two separate fields in the same table... looking
I have a table, users, in an Oracle 9.2.0.6 database. Two of the fields
I have a table with two decimal(18,0) fields. I am inserting into this table,
I have two tables, and want to update fields in T1 for all rows
I have a table in MySQL that has 3 fields and I want to

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.