Get Latest ID from a Duplicate Records in a table Get Latest ID from a Duplicate Records in a table sql sql

Get Latest ID from a Duplicate Records in a table


select R.id,       R.groupid,       R.name,       R.codefrom (select id,              groupid,              name,              code,             row_number() over(partition by name, code order by groupid desc) as rn      from RawTable            ) as Rwhere R.rn = 1     

Or if you don't have row_number()

select R1.id,       R1.groupid,       R1.name,       R1.codefrom RawTable as R1  inner join (                select name, code, max(groupid) as groupid              from RawTable              group by name, code             ) as R2    on R1.name = R2.name and       R1.code = R2.code and       R1.groupid = R2.groupid


Try this way, it will give you max group id which will be latest :

SELECT MAX(GroupId), Name, Code FROM RAWtableGROUP BY Name, Code 


select  max(id),name, code from RaTablegroup by name,code having count(*)>1 

Will return:

id  name    code2   Name1   Code1

Will return the max gorupid for all the records that have more than one record in the table