By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,934 Members | 1,692 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,934 IT Pros & Developers. It's quick & easy.

How to get the last N records from a table

P: n/a
Hi all,

I am trying to come up with a sql query (just one query) that returns
all the columns for the last N records that were inserted into a
table. I have a column in the table that has the creation timestamp
for each record. In other words, if a client requests to get only the
latest 5 records, then this query shoud exactly do that: get all the
columns for the last 5 records that were inserted into the table.
Nov 12 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Ricardo" <r_********@yahoo.com> wrote in message
news:ab**************************@posting.google.c om...
Hi all,

I am trying to come up with a sql query (just one query) that returns
all the columns for the last N records that were inserted into a
table. I have a column in the table that has the creation timestamp
for each record. In other words, if a client requests to get only the
latest 5 records, then this query shoud exactly do that: get all the
columns for the last 5 records that were inserted into the table.


ORDER BY the creation timestamp column in DESCENDING sequence. Then use the
FIRST n ROWS ONLY clause.
Nov 12 '05 #2

P: n/a
Don't forget to have an index if possible to attempt to reduce table access.

--

Bob
Consulting I/T Specialist
IBM Toronto Lab
IBM Software Services for Data Management
[My comments are solely my own and are not meant to represent an official IBM position - ask my cat!]
"Mark A" <ma@switchboard.net> wrote in message news:jL******************@news.uswest.net...
"Ricardo" <r_********@yahoo.com> wrote in message
news:ab**************************@posting.google.c om...
Hi all,

I am trying to come up with a sql query (just one query) that returns
all the columns for the last N records that were inserted into a
table. I have a column in the table that has the creation timestamp
for each record. In other words, if a client requests to get only the
latest 5 records, then this query shoud exactly do that: get all the
columns for the last 5 records that were inserted into the table.


ORDER BY the creation timestamp column in DESCENDING sequence. Then use the
FIRST n ROWS ONLY clause.
Nov 12 '05 #3

P: n/a
> > Hi all,

I am trying to come up with a sql query (just one query) that returns
all the columns for the last N records that were inserted into a
table. I have a column in the table that has the creation timestamp
for each record. In other words, if a client requests to get only the
latest 5 records, then this query shoud exactly do that: get all the
columns for the last 5 records that were inserted into the table.
ORDER BY the creation timestamp column in DESCENDING sequence. Then use

the FIRST n ROWS ONLY clause.

You should probably create an index on the creation timestamp column and use
the ALLOW REVERSE SCANS clause on the CREATE INDEX.
Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.