简体   繁体   中英

How to select records which have the highest version without getting the old versions record?

I have below single table, in which we have same 3 rocords with differnt version, We need to fetch records with their highest version.

table

Id  name   value   verion
1   raj    java      8
2   raj    stack     4
3   prem   abc       6
4   raj    google    1
5   prem   pqr       11

and many more like this

Output should look like this

Id  name   value   verion
1   raj    java      8
5   prem   pqr       11

I have already tried to find the solution with below Stack over question, but I am using single table, not able to find the solution

Selecting most recent and specific version in each group of records, for multiple groups

Using subquery we can find max verion per name.

select m.*
from my_table m
inner join (select name,
                   max(verion) as mx_ver
            from my_table 
            group by name
           ) as mx_ on mx_.name=m.name and mx_.mx_ver=m.verion;

https://dbfiddle.uk/NavF5X_K

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM