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

How to select last 10 records from the table?


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

New User


Joined: 16 Jul 2005
Posts: 15
Location: India - Chennai

PostPosted: Sat Jul 16, 2005 3:59 pm
Reply with quote

HI,

I have to display last 10 records and

3rd to 5 th record from the same table.

How can i do ?
Back to top
View user's profile Send private message
parikshit123

Active User


Joined: 01 Jul 2005
Posts: 269
Location: India

PostPosted: Mon Jul 18, 2005 5:40 pm
Reply with quote

Hi Abirami.YN,

In RDBMS, there is nothing like Nth record.

so, retriving 10 last records from a DB2 table doesn't make any sence.
Back to top
View user's profile Send private message
Abirami.YN

New User


Joined: 16 Jul 2005
Posts: 15
Location: India - Chennai

PostPosted: Tue Jul 19, 2005 6:32 pm
Reply with quote

HI Parikshit chudasma

We can retrive using the following code.
But i am getting -104 error.
I think this because of DB2 Version ( Iam not sure)

select * from (
select rownumber() over(order by fieldname) as r
from tablename
) as temptablename where r<= 10
Back to top
View user's profile Send private message
Prasanthhere

Active User


Joined: 03 Aug 2005
Posts: 306

PostPosted: Wed Aug 03, 2005 3:15 pm
Reply with quote

Try this

Select * from table name fetch last 10 rows
Back to top
View user's profile Send private message
radhakrishnan82

Active User


Joined: 31 Mar 2005
Posts: 435
Location: chennai, India

PostPosted: Thu Aug 04, 2005 10:05 am
Reply with quote

Fetch last 10 rows will do.
Back to top
View user's profile Send private message
ovreddy

Active User


Joined: 06 Dec 2004
Posts: 211
Location: Keane Inc., Minneapolis USA.

PostPosted: Fri Aug 05, 2005 2:35 pm
Reply with quote

Hi,


SELECT * FROM <<TABLE NAME>> ORDER BY <<Primary Key>> DESC FETCH FIRST 10 ROWS ONLY;;

It will work. Don't say that u don't have a Primary Key. If So how can you say that these are last 10 records. If you do not agree on this give me a real time situation where we need this.

If you say that it is asked in some X Interview then ask them for a real situation. If they are really good enough about CODD'S rules they will agree with you.

Thanks,
Reddy.
Back to top
View user's profile Send private message
thiagold

New User


Joined: 29 Nov 2005
Posts: 3

PostPosted: Tue Nov 29, 2005 8:25 pm
Reply with quote

ovreddy wrote:

SELECT * FROM <<TABLE NAME>> ORDER BY <<Primary Key>> DESC FETCH FIRST 10 ROWS ONLY;;


That way do not work ins DB2 V6, only V7 or +. Anybody knows how to do it in DB2 V6??
Back to top
View user's profile Send private message
dgrinch

New User


Joined: 16 Nov 2005
Posts: 4

PostPosted: Tue Nov 29, 2005 11:30 pm
Reply with quote

Not sure if it's for batch or online that u're asking. Ofcourse u might be using a Curosr, so use clauses DESC & OPTIMIZE FOR n ROWS assuming you will give a proper primary key values. The same can be done for first 5 rows and programatically maniplulate 3 & 5 th row. But 3rd & 5th row manipulation kinda strikes me odd. It doesn't really happen that way. Can you elaborate a bit on what you're trying to do? Hope this helps.
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 Compare 2 files and retrive records f... DFSORT/ICETOOL 3
No new posts Compare 2 files(F1 & F2) and writ... JCL & VSAM 8
No new posts Load new table with Old unload - DB2 DB2 6
No new posts Compare only first records of the fil... SYNCSORT 7
No new posts Pulling a fixed number of records fro... DB2 2
Search our Forums:

Back to Top