473,320 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to get the last N records from a table

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
3 10069
"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
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
> > 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: gsb | last post by:
I'm not very good in mySQL but am trying to modify a script. It has to do with a log table. I want to retrieve the last 2 log records (if any) and if the last log entries are the same as my...
12
by: francisds | last post by:
Hi, Can you guys see if there's a solution to this problem? I have a database from which I have to read each record and process that record. New records are being added all the time, so I...
20
by: Guru | last post by:
Hi I have a table which contains number of rows. I want to fetch the last 5 records from the table. I know for the first 'n' records we can use FETCH FIRST n ROWS ONLY command. But i want to...
2
by: PMB | last post by:
Thank you in advance for any and all assistance. I'm trying to keep my numbers for transactions sequential. I clear my monthly transactions each month and store them in a general transactions...
3
by: AndyBell | last post by:
Hi all! I have an Access 2000 database for the Habiat for Humanity where I work. This is the second database I have written and it gets a bit more complex each time... I have learned much and...
6
by: MLH | last post by:
I have a table - tblCorrespondence. It houses records of correspondence initiated here in our office. Records are appended whenever it is recognized that some type of outbound letter needs to be...
2
by: Luis P. Mendes | last post by:
Hi, I would like to know if there is a better way to do what I'm already doing as stated in the following example, when using psycopg2 with PostgresQL. ........................ nr_bars_before =...
6
by: Christo | last post by:
I have this script for showing news on a page, but i want it to only show the last 10 records, as in the 10 records that were added to the database last. the script shows the entries in descending...
1
by: BurtonBach | last post by:
I have a query that selects any records in a table that have to do with units ordered. I would like to have a nested query that then selects the last three records from the above query. Here is...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.