Results 1 to 3 of 3
  1. #1
    hawkins is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Posts
    82

    Query to find the second largest year

    I have a table called WPM which containts quite a few columns. The important columns are Years and Tested. I want to create a query that will search for the record that holds the second largest year and in the Years column and display both the year and the tested value. So in short, I need a query to search for the record that holds in second largest year in the table WPM. Ive been able to select the ones that are not the greatest year but it still shows the 3rd and 4th values. Name4ID is used for relationship reasons and the Static.FdrName is used to only allow those selected for the specific name. The code I used is:
    SELECT Max(WPM.Years) AS MaxOfYears, Static.FdrName, WPM.Tested, WPM.Name4ID
    FROM Static INNER JOIN WPM ON Static.NameID = WPM.Name4ID
    WHERE (((WPM.[Years])<(select max(Years) from WPM)))
    GROUP BY Static.FdrName, WPM.Tested, WPM.Name4ID
    HAVING (((Static.FdrName)=[Reports]![MainRep]![FdrName]));

    Any suggestions are greatly appreciated.

  2. #2
    TheShabz is offline Court Jester
    Windows XP Access 2003
    Join Date
    Feb 2010
    Posts
    1,368
    give this a go. What I did was look for the smallest year from the subquery that pulls the two highest years.

    SELECT Min(WPM.Years) AS MinOfYears, Static.FdrName, WPM.Tested, WPM.Name4ID
    FROM Static INNER JOIN WPM ON Static.NameID = WPM.Name4ID
    WHERE WPM.Years In (SELECT DISTINCT TOP 2 WPM.Years FROM WPM ORDER BY Years DESC) AND Static.FdrName=[Reports]![MainRep]![FdrName]

    On a sidenote, try to avoid using "year" and "years" as table or field names. Access reserves certain words and if you use them as object names, you could run into issues later on down the line.
    Last edited by TheShabz; 07-05-2011 at 02:31 PM.

  3. #3
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    one way you can do this is to call a function in vba. so, just off the top of my head, you could write this:

    Code:
    SELECT Static.FdrName, WPM.Tested, WPM.Name4ID
    FROM Static INNER JOIN WPM ON Static.NameID = WPM.Name4ID
    WHERE [Years] = (((GetSecondMaxYear(tableName, fieldName))))
    GROUP BY Static.FdrName, WPM.Tested, WPM.Name4ID
    HAVING (((Static.FdrName)=[Reports]![MainRep]![FdrName]));
    and the function that is called with the above sql would be:

    Code:
    function GetSecondMaxYear(tableName, fieldName) as date
    
    dim rs as dao.recordset
    set rs = currentdb.openrecordset("select distinct [" & _
                                          fieldName & "] FROM " & _
                                          tableName & " ORDER BY [" & _
                                          fieldName & "] DESC")
    
    rs.movelast
    rs.movefirst
    rs.movenext
    
    GetSecondMaxYear = rs.fields(0)
    
    rs.close
    
    set rs = nothing
    
    end function

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Query can't find Year data
    By G.King in forum Queries
    Replies: 7
    Last Post: 06-23-2011, 03:07 PM
  2. find the closest year
    By 12345678 in forum Queries
    Replies: 1
    Last Post: 05-25-2011, 05:07 AM
  3. How to change year digit in update query?
    By Osman in forum Queries
    Replies: 6
    Last Post: 10-27-2010, 04:35 AM
  4. Forecast Query with stepwise increasing year
    By theracer06 in forum Queries
    Replies: 1
    Last Post: 08-12-2010, 06:08 PM
  5. Query to extract record of particular year
    By pkg206 in forum Access
    Replies: 2
    Last Post: 11-11-2009, 10:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums