Results 1 to 2 of 2
  1. #1
    msadiqrajani is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2012
    Posts
    12

    Query for extracting latest year data for each country

    Hi,


    I have big dataset in following manner.
    Country Year Value
    Pakistan 2000 4
    Srilanka 2001 2
    Australia 2001 3
    Srilanka 2002 2
    Pakistan 2002 5
    Australia 2000 7
    China 1999 4
    Pakistan 2001 5
    China 2000 7
    Srilanka 2000 8

    I need to extract data for each country for latest year...
    So final dataset I will get will be
    Country Latest Year Value
    Pakistan 2002 5
    Srilanka 2002 2
    China 2000 7
    Australia 2001 3


    What will be the query to extract latest year for each country...

  2. #2
    recyan's Avatar
    recyan is offline Expert
    Windows 2K Access 2000
    Join Date
    Dec 2011
    Posts
    662
    Just check out if below gives some guidelines :
    Code:
    SELECT 
        qryCountryMaxYearA.Country, 
        qryCountryMaxYearA.MaxOfTheYear, 
        myTable.Country, 
        myTable.TheYear, 
        myTable.TheValue
    FROM 
        ( 
            SELECT 
                myTable.Country, 
                Max(myTable.TheYear) AS MaxOfTheYear
            FROM 
                myTable
            GROUP BY 
                myTable.Country
        ) 
        AS qryCountryMaxYearA 
        INNER JOIN 
        myTable 
        ON 
        (qryCountryMaxYearA.MaxOfTheYear = myTable.TheYear) 
        AND 
        (qryCountryMaxYearA.Country = myTable.Country);
    Thanks

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

Similar Threads

  1. Query Expression to return only the latest data
    By Brinleigh217 in forum Queries
    Replies: 6
    Last Post: 03-23-2012, 02:53 PM
  2. extracting data from field
    By focosi in forum Queries
    Replies: 6
    Last Post: 02-11-2012, 03:12 AM
  3. Query can't find Year data
    By G.King in forum Queries
    Replies: 7
    Last Post: 06-23-2011, 03:07 PM
  4. Extracting text from XML data
    By rob4465 in forum Access
    Replies: 1
    Last Post: 03-18-2010, 06:41 PM
  5. Extracting data after a keyword
    By KerryA in forum Queries
    Replies: 1
    Last Post: 02-11-2010, 09:56 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