Results 1 to 6 of 6
  1. #1
    markjkubicki's Avatar
    markjkubicki is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Location
    Fire Island Pines, NY
    Posts
    496

    return last value in a sorted query / table

    i'm looking for the field value of the last record in a query / table with an SQL sorted order (I presume that this is differnet (?) than the MAX() value of the field, which may not be the value of the last record.)



    is there a command line that will get this to me (or do I need to use a series opf steps: requery, go to last record, get data, and so forth...) ?

    many thanks in advance,
    mark

  2. #2
    jzwp11 is offline VIP
    Windows 7 Access 2007
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    Welcome to the Forum!

    If you are using a sequential autonumber primary key field, you can get the last record added using an aggregate query nested within another query


    SELECT fieldofinterest
    FROM yourtable
    WHERE keyfield IN (SELECT Max(autonumberfield) from yourtable)

    Alternatively, in you could create another query based on your sorted query and use the following syntax

    SELECT Top 1 field1, field2...
    FROM yoursortedquery

  3. #3
    markjkubicki's Avatar
    markjkubicki is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Location
    Fire Island Pines, NY
    Posts
    496

    I may need to clarify

    I'm trying to get the data from the record which would be at the bottom of the sorted query / table; it may not be the last record entered.

    for ex:
    record # value
    1 A
    2 C
    3 B

    if the sort order is alphabetical / ascending, I would want the result of record #2, "C"
    if the sort order was descending, the result would be record #1, "A"

    I should also note that my sort order is not a "simple" (ex: alpha, numeric, ...), and requires a a SQL query...

  4. #4
    jzwp11 is offline VIP
    Windows 7 Access 2007
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    Reverse the sort order of your sorting query such that the last record becomes the first record and then use the alternative option I proposed:

    SELECT Top 1 field1, field2...
    FROM yoursortedquery

    SQL does not have a Bottom predicate so you have to reverse the order and use the Top predicate.

  5. #5
    markjkubicki's Avatar
    markjkubicki is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Location
    Fire Island Pines, NY
    Posts
    496
    Quote Originally Posted by jzwp11 View Post
    Reverse the sort order of your sorting query such that the last record becomes the first record and then use the alternative option I proposed:

    SELECT Top 1 field1, field2...
    FROM yoursortedquery

    SQL does not have a Bottom predicate so you have to reverse the order and use the Top predicate.
    thnx for the tip

  6. #6
    jzwp11 is offline VIP
    Windows 7 Access 2007
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    Let us know if what I proposed works for you. If not, there may be another approach, but we would have to get a better understanding of your data and your table structure.

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

Similar Threads

  1. Replies: 1
    Last Post: 05-17-2010, 12:21 PM
  2. Some values in report did not sorted
    By Akirien in forum Reports
    Replies: 1
    Last Post: 04-14-2010, 11:31 PM
  3. Replies: 2
    Last Post: 02-26-2010, 08:53 AM
  4. Multiple Query Results Sorted Together
    By Rawb in forum Reports
    Replies: 1
    Last Post: 12-10-2009, 04:05 PM
  5. Replies: 1
    Last Post: 02-05-2009, 04:53 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