IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

I want the last record of the perticular set of rows.


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
jeroznishanth

New User


Joined: 21 Nov 2007
Posts: 8
Location: Bangalore

PostPosted: Mon Sep 14, 2009 12:50 pm
Reply with quote

I have to search a set of rows with a particular rule.

Eg: rows whose name = xyz.

Then I will get some 5 rows.

Id name
2 xyz
3 xyz
4 xyz
5 xyz

now I want to fetch only the last record.

that is

Id name
5 xyz

Id is a varchar


Please provide me the one single query to execute this.

thnaks in advance
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Mon Sep 14, 2009 12:55 pm
Reply with quote

did You sarch the forums, looks like not,
if You had you would have found many topics dicussing the issue
Back to top
View user's profile Send private message
Escapa

Senior Member


Joined: 16 Feb 2007
Posts: 1399
Location: IL, USA

PostPosted: Mon Sep 14, 2009 1:31 pm
Reply with quote

Code:
select Id name from tablename order by Id desc fetch first 1 rows only
Back to top
View user's profile Send private message
arvind.m

Active User


Joined: 28 Aug 2008
Posts: 205
Location: Hyderabad

PostPosted: Mon Sep 14, 2009 1:41 pm
Reply with quote

Hi Sambhaji,

i have a question. If you use ORDER BY caluse will that won't change the order and gives a differet output (than what expected).
Back to top
View user's profile Send private message
Escapa

Senior Member


Joined: 16 Feb 2007
Posts: 1399
Location: IL, USA

PostPosted: Mon Sep 14, 2009 1:44 pm
Reply with quote

Quote:
i have a question. If you use ORDER BY caluse will that won't change the order and gives a differet output (than what expected).

Thats what we wanted here if I have understood the requirement..

Can you explain with example what you wanted to say?
Back to top
View user's profile Send private message
arvind.m

Active User


Joined: 28 Aug 2008
Posts: 205
Location: Hyderabad

PostPosted: Mon Sep 14, 2009 1:55 pm
Reply with quote

The input provided is in the sorted order. But in real time the SELECT statement may or maynot be in the sorted order. So if the SELECT stmt is not in the sorted order the last record won't be the same as when you provide
Quote:

order by Id desc fetch first 1 rows only
Back to top
View user's profile Send private message
Bharath Bhat

Active User


Joined: 20 Mar 2008
Posts: 283
Location: chennai

PostPosted: Mon Sep 14, 2009 2:00 pm
Reply with quote

arvind.m wrote:
Hi Sambhaji,

i have a question. If you use ORDER BY caluse will that won't change the order and gives a differet output (than what expected).



If you do not use order by, you don't know what to expect. DB2 does not retrive the rows in any particular order unless you specify an ORDER BY.
Back to top
View user's profile Send private message
Anuj Dhawan

Superior Member


Joined: 22 Apr 2006
Posts: 6250
Location: Mumbai, India

PostPosted: Mon Sep 14, 2009 4:19 pm
Reply with quote

arvind.m wrote:
If you use ORDER BY caluse will that won't change the order and gives a differet output (than what expected).
Without an ONDER BY You just arbitrarily selected a row. It could be different the next time you do it, or it could be the same for the life of the table. Again, this is a complete waste of a query without an ORDER BY clause.
Back to top
View user's profile Send private message
lkhiger

New User


Joined: 28 Oct 2005
Posts: 89

PostPosted: Mon Sep 14, 2009 5:01 pm
Reply with quote

Use this query, if you want:

Code:
select * from table1
where id = (select max(id) from table1)
fetch first row only


Lenny
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Mon Sep 14, 2009 5:10 pm
Reply with quote

the query would be right if the topic starter had specified a reasonable rule...

asking for the last one without any concern for the order,max, min or any other sql compliant constraint is just poor understanding of how sql works
Back to top
View user's profile Send private message
arvind.m

Active User


Joined: 28 Aug 2008
Posts: 205
Location: Hyderabad

PostPosted: Mon Sep 14, 2009 6:38 pm
Reply with quote

Yes. i agree with enrico.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts How to split large record length file... DFSORT/ICETOOL 10
No new posts To get the count of rows for every 1 ... DB2 3
No new posts SFTP Issue - destination file record ... All Other Mainframe Topics 2
No new posts FINDREP - Only first record from give... DFSORT/ICETOOL 3
No new posts To find whether record count are true... DFSORT/ICETOOL 6
Search our Forums:

Back to Top