473,499 Members | 1,948 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to select the last row of the resultset

hi, we are running db2 udb on v8.1, i am using jdbc sql, is there
anyway to create a sql saying i want to get the last record of
resultset? i try to open a big resultset and point the cursor to the
last (like select * from table, then do resultset.last), i only want
to get a one row back which is the last row of the resultset. thanks
Nov 12 '05 #1
5 46944
Can you simply produce the last row only in teh first place?
That would be the best.

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #2
To get the first row:

select * from address order by name asc fetch first 1 row only

To get the last row, order by the same column in reverse (descending)
order:

select * from address order by name desc fetch first 1 row only

xixi wrote:
hi, we are running db2 udb on v8.1, i am using jdbc sql, is there
anyway to create a sql saying i want to get the last record of
resultset? i try to open a big resultset and point the cursor to the
last (like select * from table, then do resultset.last), i only want
to get a one row back which is the last row of the resultset. thanks


Nov 12 '05 #3
What about you
.... order by x descending fetch first 1 rows for read only?

PM

"xixi" <da****@yahoo.com> a écrit dans le message de
news:c0**************************@posting.google.c om...
hi, we are running db2 udb on v8.1, i am using jdbc sql, is there
anyway to create a sql saying i want to get the last record of
resultset? i try to open a big resultset and point the cursor to the
last (like select * from table, then do resultset.last), i only want
to get a one row back which is the last row of the resultset. thanks

Nov 12 '05 #4
xixi wrote:
hi, we are running db2 udb on v8.1, i am using jdbc sql, is there
anyway to create a sql saying i want to get the last record of
resultset? i try to open a big resultset and point the cursor to the
last (like select * from table, then do resultset.last), i only want
to get a one row back which is the last row of the resultset. thanks


I have always been confused by people wanting to do things like this,
because DB2 uses the relational model wherein the data are a set, not an
ordered set. So the idea of a "last row" is meaningless. So it escapes me
why people go out of their way to impose an ordering without defining the
ordering.

As a practical matter, it seems that DB2 would return rows in response to
a query (that does not have ORDER BY) in about the order they were entered
(if no reorganization has taken place) or in the order in which they were
presently stored. But it does not matter much which, as far as I can tell.

I am not saying it is not a possible solution to some problem or other.
But it seems to me that since the concept of "last row" is at best
undefined, it would seem to make more sense to define what is wanted and
retrieve that.

So if you want a "last row", why not do an ORDER BY ... and arrange for
the "last row" to come out first. You could even have the query contain
stuff like:

ORDER BY column1 DESC, column2 ASC ...
FOR READ ONLY
OPTIMIZE FOR 1 ROWS;

or something like that. If there is no such column or columns, put one in,
if only temporarily.

--
.~. Jean-David Beyer Registered Linux User 85642.
/V\ Registered Machine 241939.
/( )\ Shrewsbury, New Jersey http://counter.li.org
^^-^^ 09:15:00 up 4 days, 16:30, 3 users, load average: 3.47, 3.94, 4.03

Nov 12 '05 #5
It does not matter, and it is not dependable - if you have no order by,
the result set is arbitrary. Rows stored on different devices, on
different machines with different CPU speeds, rows that have been
re-org'd - all of these things make order unpredictable without an ORDER
BY.

FWIW, it seems to be the java crowd presenting rows on web pages who
care most about adding arbitrary row numbers.

Jean-David Beyer wrote:
...

As a practical matter, it seems that DB2 would return rows in response
to a query (that does not have ORDER BY) in about the order they were
entered (if no reorganization has taken place) or in the order in which
they were presently stored. But it does not matter much which, as far as
I can tell.


Nov 12 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
10299
by: Ben | last post by:
I believe I am missunderstanding how subqueries work. I simple subquery works fine but when I wish do compare 2 or more fields at once I don't get the results I wish. Table A...
5
9972
by: Nick Weisser | last post by:
Hi there, I'm not sure how to select the last 3 items in ascending order. This does the trick in descending order: select * from user_menu_main where deleted = 0 and hidden = 0 order by...
8
5195
Alireza355
by: Alireza355 | last post by:
I have two tables: table1: number explanation 10 something here 11 something here 12 something here 13 something...
2
2725
ddtpmyra
by: ddtpmyra | last post by:
My query results has 3 rows but I only wanted to select the max date or the last update. How can i do this? I only wanted to pick the most current date which is the last record below: Result:...
1
1834
by: ghjk | last post by:
When I insert a new data I want to check whether user already exist and also I want to take the last inserted record number and increment it by one. How can I check both conditions in one select...
1
3042
by: karuppiah | last post by:
Hi All, i need query,how to select last 3 month ,last 3wek,last 3 days , this last 3 month and 3 week should be friday, using sysdate from dual table, there is no exsting table, pls any one help...
2
17856
by: Rohullah | last post by:
Hello for example i have 1000 records in a table and dally we insert record to that table and i want to select the last 10 rows from that table how to do it. give me the best answer. looking...
2
2398
by: minijus | last post by:
Hello, I have a query like this: SELECT jos_vm_orders.user_id, jos_vm_orders.cdate, sum( order_total ) AS total, count( jos_vm_orders.order_id ) AS ordercount, jos_vm_user_info.first_name,...
4
3605
by: ndeeley | last post by:
Hello, This is going to sound strange but I have a webpage that displays a form showing the last record added. This is because further records share a lot of the same data, so only a few fields...
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7392
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4920
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4605
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3105
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
307
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.