dbTalk Databases Forums  

Find row number x in a table

comp.databases.sybase comp.databases.sybase


Discuss Find row number x in a table in the comp.databases.sybase forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Alfred Zentek
 
Posts: n/a

Default Find row number x in a table - 11-26-2003 , 01:27 PM






Hi all,

does someone of you know how to find a special
row in a table, not using a WHERE clause?
To make it more clear: What do I have to enter if I
want to know the contents of row number six in table <abc>?

Any tricks???

Thank you very much in advance - Alfred


--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG

Reply With Quote
  #2  
Old   
Joe Weinstein
 
Posts: n/a

Default Re: Find row number x in a table - 11-26-2003 , 01:38 PM








Alfred Zentek wrote:

Quote:
Hi all,

does someone of you know how to find a special
row in a table, not using a WHERE clause?
To make it more clear: What do I have to enter if I
want to know the contents of row number six in table <abc>?

Any tricks???
The key is to define what you mean by 'row number 6'. Is this the
chronologically sixth inserted row? Is it the sixth row as defined
by a unique key? Is it just the sixth row you currently see when you
do a 'select(*)'? Does this table have any indexes? Any of them clustered?
Depending on your answers, there may be one trick or another.

Joe Weinstein at BEA
Quote:
Thank you very much in advance - Alfred




Reply With Quote
  #3  
Old   
Alfred Zentek
 
Posts: n/a

Default Re: Find row number x in a table - 11-26-2003 , 02:30 PM




Quote:
The key is to define what you mean by 'row number 6'. Is this the
chronologically sixth inserted row? Is it the sixth row as defined
by a unique key? Is it just the sixth row you currently see when you
do a 'select(*)'? Does this table have any indexes? Any of them clustered?
Depending on your answers, there may be one trick or another.
From your answer I learned a lot for the questions in the future,
thank you so far.

In fact, this table has 5 Indexes, one of them is a clustered one.
But, your guess was right I'm at the moment only interested to fetch
for example the sixth row of the table if I do a select (*).
Background information: Currently programming a data generator which
will just generate data on a random basis but relies on data in
different
tables.

Thank's in advance - Alfred


--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG


Reply With Quote
  #4  
Old   
Joe Weinstein
 
Posts: n/a

Default Re: Find row number x in a table - 11-26-2003 , 02:54 PM





Alfred Zentek wrote:

Quote:
The key is to define what you mean by 'row number 6'. Is this the
chronologically sixth inserted row? Is it the sixth row as defined
by a unique key? Is it just the sixth row you currently see when you
do a 'select(*)'? Does this table have any indexes? Any of them clustered?
Depending on your answers, there may be one trick or another.


From your answer I learned a lot for the questions in the future,
thank you so far.

In fact, this table has 5 Indexes, one of them is a clustered one.
But, your guess was right I'm at the moment only interested to fetch
for example the sixth row of the table if I do a select (*).
Background information: Currently programming a data generator which
will just generate data on a random basis but relies on data in
different
tables.
Ok. To stick to what you can expect from every DBMS, a plain
'select * from foo' is not guaranteed to return the rows in
the same order as it did the last time. Practically though,
a DBMS usually does return them in the same order. If you want
random data, you should consider the rand() function, but
to get what is curently the sixth row according to some order,
such as by your clustrered index, I would do a query like:

select * from mytable A
where 5 = (select count(*) from mytable AA where AA.myUniqueKeyCol < A.myUniqueKeyVal)

In other words, for every row in the table, we do a subquery on the same table
to cound how many rows come before the current row in the main query, and
we only return the row that has 5 rows before it in the table.

This isn't going to be fast, and for big tables it'll be a pain.

On the whole, I'd recommend another way of generating random data.
I'd consider caching the table data in files or the client, and
choosing a random index into an array. Just refresh the cached data
when needed.
Joe Weinstein at BEA
Quote:
Thank's in advance - Alfred




Reply With Quote
  #5  
Old   
Alfred Zentek
 
Posts: n/a

Default Re: Find row number x in a table - 11-28-2003 , 06:40 AM



Thank for all the tips I got - works :-)

KR Alfred


--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.