I have a table with 5 columns like this:
id, name, firstName, job, number
There are many records in this table. Now, imagine these records:
- 1 Konan toto doctor 45
- 2 Konan tata doctor 50
- 3 Konan toto doctor 60
- 4 simba popo police 44
- 5 simba tata police 88
- 6 pikar popo doctor 99
- 7 simba popo doctor 72
now I want to find only record which job is doctor and get record with no duplicate in (name,firstName) (if we have many records with same name + lastName we return only one record , let say anyone)
the result will be
- 1 Konan toto doctor 45
- 2 Konan tata doctor 50
- 6 pikar popo doctor 99
- 7 simba popo doctor 72
the record with id 3 is removed because there is duplicate on (name,firstName) the others because we only need job = doctor
What is the Hibernate Query to get the desired results ?
SELECT MAX(p.id), p.name, p.firstName, MAX(p.job), MAX(p.number) FROM Person p WHERE p.job = 'doctor' GROUP BY p.name, p.firstNameidandnumber, which may not be present in the source dataSELECT p.id, p.name, p.firstName, MAX(p.job), MAX(p.number) From Person p WHERE p.id IN (SELECT MAX(p2.id) FROM Person p2 WHERE p2.job = 'doctor' GROUP BY p.name, p.firstName)is probably more appropriate