Fetching the last few rows from a table



IBM's flagship relational database management system

Fetching the last few rows from a table

Postby riya_kottoor » Mon Mar 05, 2018 3:05 pm

Hi,

Is there a way to fetch the last few rows from a table? Suppose i want last 5 rows. How can i do this?
If i do an order by descending and fetch first 5 rows, the order may differ from the actual order in which rows were inserted into the table.

Please suggest.
riya_kottoor
 
Posts: 13
Joined: Thu Sep 21, 2017 11:58 am
Has thanked: 0 time
Been thanked: 0 time

Re: Fetching the last few rows from a table

 

Re: Fetching the last few rows from a table

Postby NicC » Mon Mar 05, 2018 4:26 pm

Have you got anything to select on like a timestamp or sequence number?
The problem I have is that people can explain things quickly but I can only comprehend slowly.
Regards
Nic
NicC
Global moderator
 
Posts: 2782
Joined: Sun Jul 04, 2010 12:13 am
Location: Pushing up the daisies (almost)
Has thanked: 4 times
Been thanked: 114 times

Re: Fetching the last few rows from a table

Postby prino » Tue Mar 06, 2018 2:30 am

riya_kottoor wrote:Hi,

Is there a way to fetch the last few rows from a table? Suppose i want last 5 rows. How can i do this?
If i do an order by descending and fetch first 5 rows, the order may differ from the actual order in which rows were inserted into the table.

And what makes you thing that just fetching the last five rows will get them in the order they were inserted?

Sigh...
Robert AH Prins
robert.ah.prins @ the.17+Gb.Google thingy
User avatar
prino
 
Posts: 562
Joined: Wed Mar 11, 2009 12:22 am
Location: Oostende, Belgium
Has thanked: 3 times
Been thanked: 24 times

Re: Fetching the last few rows from a table

Postby Robert Sample » Tue Mar 06, 2018 3:06 am

Suppose i want last 5 rows. How can i do this?
Depending upon how the database is defined, what you want may not be possible. DB2 results are NOT necessarily returned in the order inserted into the database (with or without an ORDER BY clause); hence unless you have some form of sequence number or timestamp on each row, you CANNOT know what the 'last 5 rows" are -- period.
Robert Sample
Global moderator
 
Posts: 3507
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 248 times

Re: Fetching the last few rows from a table

Postby riya_kottoor » Tue Mar 06, 2018 11:34 am

Okay. So fetch may return rows in jumbled order? Then how "FETCH FIRST n ROWS ONLY" works? we can't assure that the n rows which got fetched are the first n rows that got inserted to the table, right?
riya_kottoor
 
Posts: 13
Joined: Thu Sep 21, 2017 11:58 am
Has thanked: 0 time
Been thanked: 0 time

Re: Fetching the last few rows from a table

Postby enrico-sorichetti » Tue Mar 06, 2018 12:25 pm

the clause
FIRST n ROWS ONLY


will return the FIRST n ROWS according to the FETCH/READ LOGIC/RULES of DB2
and the logic/rule is that the without an ORDER clause the order in which the rows are fetched/read is UNPREDICTABLE
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort
enrico-sorichetti
Global moderator
 
Posts: 2728
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 140 times

Re: Fetching the last few rows from a table

Postby riya_kottoor » Tue Mar 06, 2018 2:35 pm

thank you all for making the concept clear for me.
riya_kottoor
 
Posts: 13
Joined: Thu Sep 21, 2017 11:58 am
Has thanked: 0 time
Been thanked: 0 time


Return to DB2

 


  • Related topics
    Replies
    Views
    Last post