web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

how to return 2nd last record from table with select statement

(0) ShareShare
ReportReport
Posted on by 56

Dear All,

I have one requirement. I need to return 2nd last record from table with select statement.

For example : I having 10 records.

I need 8th record only.

Please give me more shed on this.

Thanks!

Arpan Sen

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Rohin Profile Picture
    4,624 on at

    Martin Suggested answer already here.

  • Suggested answer
    Chaitanya Golla Profile Picture
    17,225 on at

    Hi,

    Could you please provide some more information on your business requirement.

    As per your mentioned details we can get 2nd last record by sorting records in descending order based on recId and get the 3rd record from loop(as suggested by Crispin) (or) else you can introduce a new field on your table and maintain numeric sequence while inserting the data and retrieve based on this number.

    Thanks,

    Chaitanya Golla

  • Suggested answer
    PA-22040759-0 Profile Picture
    6,194 on at

    Here is is crude suggestion, that shouldn't select more records than required from the database:

    static void Job10(Args _args)
    
    {
    
       CustTable   custTable;
    
       select noFetch custTable
    
           order by accountNum desc;
    
       next custTable;
    
       next custTable;
    
       next custTable;
    
       print custTable.AccountNum;
    
       pause;
    
    }
  • Suggested answer
    Martin Dráb Profile Picture
    239,124 Most Valuable Professional on at

    I wouldn't be that excited.

    • Will it really fetch less records? I don't think so - each of the record is fetched on next anyway. It's the same thing as while select does under the hood.
    • Is it easier to maintain? For example, what if you decided to get the fifth record instead of the third? Will it be readable and intent-revealing?
    • What if the query returns less records than the number of next statements? For example, if it returns nothing, you would be still calling next several times for no reason. Also, just taking the result of the last next without checking if it contains a values is dangerous.

    My answer (linked above) was in a thread about AX 2009. In AX 2012, you can consider creating a computed column with ROW_NUMBER or RANK() and filter by it.

  • PA-22040759-0 Profile Picture
    6,194 on at

    Sorry to be unclear on the "crude suggestion" part. This was just an idea and not intended to be the one and only true way to solve this problem.

  • @rp@n Profile Picture
    56 on at

    Thanks Palle,

    As you said it's working fine.

    Can you please elaborate me about "select noFetch custTable".

    Why you used noFetch before Table? 

    Please give me more shed on this.

    Thanks!
    Arpan

  • Rohin Profile Picture
    4,624 on at

    Martin,

     "In AX 2012, you can consider creating a computed column with ROW_NUMBER or RANK() and filter by it."

    can you please elaborate it more??

  • Martin Dráb Profile Picture
    239,124 Most Valuable Professional on at

    You didn't mention which part you have a problem with, so I'll assume you're not familiar with computed columns at all. In such a situation, you should use a search engine to find the relevant documentation (Walkthrough: Add a Computed Column to a View [AX 2012]) and other articles (e.g. my Subqueries in views, which even contains an example with ROW_NUMBER()).

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
CP04-islander Profile Picture

CP04-islander 16

#2
GiacomoRovai Profile Picture

GiacomoRovai 4

#3
Douglas Noel Profile Picture

Douglas Noel 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans