Results 1 to 3 of 3

Thread: Extracting the smallest age. MIN with SYSDATE?

  1. #1
    Join Date
    May 2009
    Posts
    7

    Question Extracting the smallest age. MIN with SYSDATE?

    I'm trying to get the youngest pilot's name and age but all the pilots show up:

    select pil_pilotname, round(min(sysdate-pil_brthdate)/365.25,0) "Age"
    from pilots
    group by pil_pilotname;

    What's missing?
    Thanks.

  2. #2
    Join Date
    Apr 2009
    Posts
    86
    antgaudi, What your query is doing is returning the Minimum Age of every distinct Pilot name. You need to find the Minimum Age so you can use that to filter out the row you want. One way to do that is with a Sub Query.

    I don't know Oracle SQL statements or syntax so I can't give you the exact statement. Here is the statement using DB2 syntax:

    SELECT PIL_PILOTNAME
    , YEAR(CURRENT_DATE - PIL_BRTHDATE) AS AGE
    FROM PILOTS
    WHERE CURRENT_DATE - PIL_BRTHDATE =
    (SELECT MIN(CURRENT_DATE - PIL_BRTHDATE)
    FROM PILOTS)
    ;

    Making a guess at Oracle syntax:

    SELECT PIL_PILOTNAME
    , ROUND( MIN( SYSDATE - PIL_BRTHDATE ) / 365.25, 0) "Age"
    FROM PILOTS
    WHERE SYSDATE - PIL_BRTHDATE =
    (SELECT MIN( SYSDATE - PIL_BRTHDATE)
    FROM PILOTS)
    ;

  3. #3
    Join Date
    May 2009
    Posts
    7
    I'll try that. Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •