Avatar of marcgu
marcguFlag for Sweden

asked on 

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;











 binkul.xls
Oracle DatabaseDatabases

Avatar of undefined
Last Comment
marcgu
ASKER CERTIFIED SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of awking00
awking00
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

>>order by utbildning.startdatum desc

Yes.  Thanks for the correction.  Missed that in the original questsion.
Avatar of marcgu
marcgu
Flag of Sweden image

ASKER

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 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.

81K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo