Is There a Way to Make a Query Select ONLY the Last Two Records of a Table

I need a query that returns only the last two records of a table. By last two, i mean the two most recent.

Can anyone suggest a way to do that?
RexQuality LeaderAsked:
Who is Participating?
 
Jerry_JusticeCommented:
Yes.  As long as you sort DESCending, the top two will always be those records with the highest value.
0
 
Jerry_JusticeCommented:
Easy if you have a auto-incremented Identity field.

So, for a table called Table with a Identity field named TableID:

Select Top 2 table.* from table Order By TableID DESC

The last two created are returned.
0
 
RexQuality LeaderAuthor Commented:
I do not, but i do have sequenced date field. I can apply the TOP 2 approach to that i suppose? I wasn't sure about the TOP thing...
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
RexQuality LeaderAuthor Commented:
Thank you
0
 
Jerry_JusticeCommented:
You're welcome!

It is a handy tool...  It is also often used as a trick in tables with numeric sequential Primary Keys that are not set to auto-increment (Identity).  You find the highest value with a TOP 1 query, and add one to it.  But, then, why not just use an integer identity field with an increment of 1?

That said, I use an Identity field that auto-increments on just about every table I create for a primary key.  I stopped agonizing about whether I should or not years ago..  it makes life so much simpler.

Rarely, you get a table that can use a "Natural key", like a Social Security Number or Student ID as its primary key, but like I said, it is rare.
0
 
RexQuality LeaderAuthor Commented:
Thanks.... seems like good practice
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.