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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Jerry_JusticeCommented:
Yes.  As long as you sort DESCending, the top two will always be those records with the highest value.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.