How do you get sql-statement to return only the latest record if two or more records meet the criterias
My select statement some times return more than on record for each personid (English=social security number). Instead, I would like the statement to only display the row which has the latest date in utbildning.STARTDATUM (English=START DATE).
Right now I get one hit for each enhet (English=unit) that meets the criteria.
I have also attached and xls-file with fake-information to show how it can be displayed. The first to records come from 1 and the same student.
The statement looks like this now:
SELECT DISTINCT (person.personid),
person.civilstand,
person.fornamn,
person.efternamn,
person.adr,
person.kommun,
person.kon,
person.co,
person.postnr,
person.ort,
person.landskod,
person.tfn,
person.folkbokfadr,
person.folkbokfpostnr,
person.tfnmobil,
person.folkbokfort,
person.folkbokfpostnr AS bokpn,
person.folkbokfkom,
person.folkbokflan,
TO_CHAR(person.folkbokfdatum,'YYYY-MM-DD') folkbokfdatum,
person.sortnamn,
utbildning.enhet,
utbildning.klass,
utbildning.utbvag,
utbildning.avbrottdatum,
utbildning.avbrottorsak,
utbildning.utbtyp,
utbildning.nyenhet,
utbildning.flyttadkommun,
utbildning.betygstyp,
utbildning.betygsdatum,
utbildning.startdatum
FROM person
JOIN utbildning
ON (utbildning.personid =person.personid)
AND (person.civilstand IS NULL
OR person.civilstand <>'6')
AND (person.personid LIKE '92%'
OR person.personid LIKE '93%'
OR person.personid LIKE'94%'
OR person.personid LIKE '95%')
AND person.sekel ='9'
AND person.folkbokfkom ='80'
AND person.folkbokflan ='05'
AND person.personid NOT IN
(SELECT DISTINCT (utbildning.personid)
FROM utbildning
WHERE (utbildning.personid LIKE '92%'
OR utbildning.personid LIKE '93%'
OR utbildning.personid LIKE '94%'
OR utbildning.personid LIKE '95%')
AND (utbildning.enhet LIKE'GYM%'
OR utbildning.enhet LIKE 'BIN%'
OR utbildning.enhet LIKE 'SEK%'
OR utbildning.enhet LIKE 'GRU%'
OR utbildning.enhet LIKE 'AVG%GY%'
OR utbildning.enhet LIKE 'MUS%')
)
ORDER BY person.personid;
Thanks for a real fast answer. The answer helped me to find out how to move on even tough some info was missing.
Oracle Database
Oracle is an object-relational database management system. It supports a large number of languages and application development frameworks. Its primary languages are SQL, PL/SQL and Java, but it also includes support for C and C++. Oracle also has its own enterprise modules and application server software.
Yes. Thanks for the correction. Missed that in the original questsion.