Home > Mobile >  How to find distinct records in JPA where id(Primary key) is different
How to find distinct records in JPA where id(Primary key) is different

Time:12-05

I have below records in postgres table, id:1, name:'ABC', tech:'Java' id:2, name:'DEF', tech:'DotNet' id:3, name:'ABC', tech:'Java'

when I am running findByName or findDistinctbyName , it is giving all 3 records. but my requirement is to get record 1 and 2. AnyOoe please suggest JPa query to retrieve expected result.

CodePudding user response:

Assuming you want to resolve duplicated by retaining the earliest id, you could use the following GROUP BY query:

SELECT MIN(id) AS id, name, tech
FROM yourTable
GROUP BY name, tech;
  • Related