473,624 Members | 2,562 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 47038
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.c om> a écrit dans le message de
news:c0******** *************** ***@posting.goo gle.com...
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
10306
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 ID First Last Other 1 A Z 1 2 B Y 2 3 C Z 3
5
9980
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 date desc limit 3
8
5209
Alireza355
by: Alireza355 | last post by:
I have two tables: table1: number explanation 10 something here 11 something here 12 something here 13 something here 14 something here
2
2743
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: 1038651 2007-05-11 RMARTINEZ In-Service 1038651 2007-07-03 Equip Ret Pend Return Pending 1038651 2007-03-07 Import Returned Query: SELECT ticket_number, MAX(ticket_status_history.addedDate) AS OrderDate, MAX(ticket_status_history.Tech_id) AS...
1
1841
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 statement? This is my code. $result = pg_query($dbconn, "SELECT \"No\",\"Name\" FROM \"User\" ORDER BY \"No\" ASC;"); while($row = pg_fetch_array($result)){ $Name=$row; if($Name==$FullName) { echo "User already exist!"; } }
1
3055
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 me. Regards, karuppiah.
2
18087
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 forword.
2
2417
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, jos_vm_user_info.last_name, jos_vm_user_info.phone_1, jos_vm_user_info.user_email, jos_vm_user_info.city FROM jos_vm_orders LEFT JOIN jos_vm_user_info ON jos_vm_orders.user_id = jos_vm_user_info.user_id WHERE jos_vm_orders.order_status = 'C' GROUP BY...
4
3612
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 have to be changed. I use the max(ID) to do this: <cfquery name="lastid" datasource="swwdrawings"> select * from tblDrawingsData where ID = (select max(ID) from tblDrawingsData where AddedBy = '#username#') </cfquery>
0
8231
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8672
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8614
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8471
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7153
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6107
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5561
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2603
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.