473,401 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

how to do row number query

i want to use sql query to open a resultset, say i want the cursor
point to whatever position i start with by giving a row number , so is
there anyway i can use the sql function to do that, so when the
resultset return, the first row will be the absolute row dertermine by
the row number
Nov 12 '05 #1
19 81712
xixi wrote:
i want to use sql query to open a resultset, say i want the cursor
point to whatever position i start with by giving a row number , so is
there anyway i can use the sql function to do that, so when the
resultset return, the first row will be the absolute row dertermine by
the row number


I don't understand your question, but you can use the row_number() function
to add a unique number for each row in the result set.

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #2
xixi wrote:
i want to use sql query to open a resultset, say i want the cursor
point to whatever position i start with by giving a row number , so is
there anyway i can use the sql function to do that, so when the
resultset return, the first row will be the absolute row dertermine by
the row number


I don't understand your question, but you can use the row_number() function
to add a unique number for each row in the result set.

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #3
Also look into "scrollable cursors".

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #4
Also look into "scrollable cursors".

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #5
what i need to do is similar the way relative record number on AS400,
say i want the cursor point to row number 5, so i get to open the
result set by some kind of function , tell the resultset i want the
cursor point to record number 5 (absolute record number), but i can't
find row_number function being available on DB2. can you tell me how
exactly to row_number function with sql
Nov 12 '05 #6
what i need to do is similar the way relative record number on AS400,
say i want the cursor point to row number 5, so i get to open the
result set by some kind of function , tell the resultset i want the
cursor point to record number 5 (absolute record number), but i can't
find row_number function being available on DB2. can you tell me how
exactly to row_number function with sql
Nov 12 '05 #7
xixi wrote:
what i need to do is similar the way relative record number on AS400,
say i want the cursor point to row number 5, so i get to open the
result set by some kind of function , tell the resultset i want the
cursor point to record number 5 (absolute record number), but i can't
find row_number function being available on DB2. can you tell me how
exactly to row_number function with sql


Exactly what is the meaning of "row number" in - by definition unordered
- set?

Technically speaking same query may produce result set in any arbitrary
order - unles *you specifically* request ORDER BY to present rows in
specific order. Within that order - duplicate rows may be presented
again in any arbitrary and undetermined order.

So if you are speaking about SQL data - you are speaking about "rows"
which are members in a set.

When you use wording "absolute record number" - you are talking flat
file records and COBOL way to access them.

You can "number" rows in an answer set by using row_number() function -
as Serge suggested. This numbeing is a tmporary mapping in a given
instance of an answer set.

Jan M. Nelken
Nov 12 '05 #8
xixi wrote:
what i need to do is similar the way relative record number on AS400,
say i want the cursor point to row number 5, so i get to open the
result set by some kind of function , tell the resultset i want the
cursor point to record number 5 (absolute record number), but i can't
find row_number function being available on DB2. can you tell me how
exactly to row_number function with sql


Exactly what is the meaning of "row number" in - by definition unordered
- set?

Technically speaking same query may produce result set in any arbitrary
order - unles *you specifically* request ORDER BY to present rows in
specific order. Within that order - duplicate rows may be presented
again in any arbitrary and undetermined order.

So if you are speaking about SQL data - you are speaking about "rows"
which are members in a set.

When you use wording "absolute record number" - you are talking flat
file records and COBOL way to access them.

You can "number" rows in an answer set by using row_number() function -
as Serge suggested. This numbeing is a tmporary mapping in a given
instance of an answer set.

Jan M. Nelken
Nov 12 '05 #9
Take a look at ROW_NUMBER() OVER(...)

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #10
Take a look at ROW_NUMBER() OVER(...)

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #11
here is the definition of rrn() function on iSeries DB2,

The RRN function returns the relative record number of a row.

and here is the example
Return the relative record number and employee name from table
EMPLOYEE for those employees in department 20.
SELECT RRN(EMPLOYEE), LASTNAME
FROM EMPLOYEE
WHERE DEPTNO = 20

so we don't care about order by, we want to know the row number of
this table meet the criteria, is DB2 UDB on windows ( i am using db2
v8.1) has same function? thanks
Nov 12 '05 #12
here is the definition of rrn() function on iSeries DB2,

The RRN function returns the relative record number of a row.

and here is the example
Return the relative record number and employee name from table
EMPLOYEE for those employees in department 20.
SELECT RRN(EMPLOYEE), LASTNAME
FROM EMPLOYEE
WHERE DEPTNO = 20

so we don't care about order by, we want to know the row number of
this table meet the criteria, is DB2 UDB on windows ( i am using db2
v8.1) has same function? thanks
Nov 12 '05 #13
xixi wrote:
here is the definition of rrn() function on iSeries DB2,

The RRN function returns the relative record number of a row.
What is the "relative record number"? Relative to what?
and here is the example

Return the relative record number and employee name from table
EMPLOYEE for those employees in department 20.
SELECT RRN(EMPLOYEE), LASTNAME
FROM EMPLOYEE
WHERE DEPTNO = 20

so we don't care about order by, we want to know the row number of
this table meet the criteria, is DB2 UDB on windows ( i am using db2
v8.1) has same function? thanks


I really don't know exactly what you want to have in the result, but you
could easily do this:

SELECT rrn, lastname
FROM ( SELECT row_number() over (), lastname, deptno
FROM employee ) AS t(rrn, lastname, deptno)
WHERE deptno = 20
If you want to get a "row number" that indicates the relative position of
the physical row with respect to the beginning of the table, then you
should be aware that those numbers might change quite easily on
insert/update/delete operations, reorgs, ...

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #14
xixi wrote:
here is the definition of rrn() function on iSeries DB2,

The RRN function returns the relative record number of a row.
What is the "relative record number"? Relative to what?
and here is the example

Return the relative record number and employee name from table
EMPLOYEE for those employees in department 20.
SELECT RRN(EMPLOYEE), LASTNAME
FROM EMPLOYEE
WHERE DEPTNO = 20

so we don't care about order by, we want to know the row number of
this table meet the criteria, is DB2 UDB on windows ( i am using db2
v8.1) has same function? thanks


I really don't know exactly what you want to have in the result, but you
could easily do this:

SELECT rrn, lastname
FROM ( SELECT row_number() over (), lastname, deptno
FROM employee ) AS t(rrn, lastname, deptno)
WHERE deptno = 20
If you want to get a "row number" that indicates the relative position of
the physical row with respect to the beginning of the table, then you
should be aware that those numbers might change quite easily on
insert/update/delete operations, reorgs, ...

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #15
Relative record numbers identify the positions of the records relative
to
the beginning of the file. For example, the relative record
numbers of
the first, fifth, and seventh records are 1, 5, and 7,
respectively. so i don't select the relative record number, i need to
use relative record number in where clause to retrieve the row like
this sql worked on AS400:

select * from table where rrn(table)=1, please help
Nov 12 '05 #16
Relative record numbers identify the positions of the records relative
to
the beginning of the file. For example, the relative record
numbers of
the first, fifth, and seventh records are 1, 5, and 7,
respectively. so i don't select the relative record number, i need to
use relative record number in where clause to retrieve the row like
this sql worked on AS400:

select * from table where rrn(table)=1, please help
Nov 12 '05 #17
is there anybody have solutions to achieve this by use sql query to
get the data for the row number i passed , which means if i want the
10th row of the data, i can select from it? thanks
da****@yahoo.com (xixi) wrote in message news:<c0**************************@posting.google. com>...
Relative record numbers identify the positions of the records relative
to
the beginning of the file. For example, the relative record
numbers of
the first, fifth, and seventh records are 1, 5, and 7,
respectively. so i don't select the relative record number, i need to
use relative record number in where clause to retrieve the row like
this sql worked on AS400:

select * from table where rrn(table)=1, please help

Nov 12 '05 #18
Ian
xixi wrote:
is there anybody have solutions to achieve this by use sql query to
get the data for the row number i passed , which means if i want the
10th row of the data, i can select from it? thanks


A table in of itself has no order, but you can write a query to
get the 10th row of an ordered result set:

select
name,
score
from (
select
name,
score,
rownumber() over (order by score desc) as rn
from
test_scores
) a
where
a.rn = 10;

If you do not specify an order for the rownumber() function, you are
not guaranteed to get the same answer each time you execute the query.

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Nov 12 '05 #19
xixi wrote:
is there anybody have solutions to achieve this by use sql query to
get the data for the row number i passed , which means if i want the
10th row of the data, i can select from it? thanks

There is no 10th row fo the data. Relations are _unordered sets_. If you
wish to number all the rows (by establishing an attribute with row number
as a value), then I suggest you do so, and make an index on it. Then you
could retrive any row number you wish efficiently.

Of course that opens up a can of worms (Remember Edsel Murphy's Law? When
you open a can of worms, to recan them takes a larger size can.): what if
you have 100 rows in a relation numbered 1 to 100. Now you delete row
numbered 42. Now what is the 50th row? What do you mean by it? Because if
you do not mean the row with the number 50 in it, you will have to
renumber all the rows after the delete. Hardly efficient.

Easier to remember that relations are unordered sets and there is no such
thing as row # N. You can order them with ORDER BY in a SELECT statement
and count your way. Or if you are lucky, you can diddle your ORDER BY so
that the row you want is first.

--
.~. Jean-David Beyer Registered Linux User 85642.
/V\ Registered Machine 241939.
/( )\ Shrewsbury, New Jersey http://counter.li.org
^^-^^ 21:00:00 up 3 days, 5:59, 6 users, load average: 4.17, 4.15, 4.13

Nov 12 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: xixi | last post by:
i want to use sql query to open a resultset, say i want the cursor point to whatever position i start with by giving a row number , so is there anyway i can use the sql function to do that, so...
1
by: David Barger | last post by:
Greetings, It appears that an Append Query I run in Access XP is randomly failing to append a field. I have payroll data being entered into a payroll database. This data is exported daily to...
2
by: nettid1 | last post by:
I'm a little new to Access so please bear with me... I have a query in Access consisting of three fields: Account Number, Query ID and Description. I want to have a button on a form that calls...
2
by: bobabooey2k | last post by:
I have an update query with one field having in its "Update to" cell a DLookup statement. This query takes 2-3 minutes on 3000 records. Can I avoid dlookup here using multiple queries? An...
10
by: BillCo | last post by:
I've been fishing in the collective knowledge pool of cdma posts gone by for a solution to this for a while - but no joy. Needless to say, I've also done my own head in trying to figure this out. ...
6
by: ngweixiong | last post by:
Hi, I have 2 query in Ms Access. Query1: Summary of Invoice (ZEAVI) Fields: Biling doc Count of biling doc Date Order number Count of Order number
3
by: clickingwires | last post by:
Is there anyway to number a query's rows? Like there are 5 records and you add a field to the query that counts them, 1, 2, 3, ...
2
by: wizdom | last post by:
I'm trying to add simple page support, the page is php/mssql based. Anyone know why this doesn't work? I found the "NOT IN" command online but I'm not 100% sure about it..... any help is...
11
by: browndudley | last post by:
I searched the forum and found something very close to helping me with what I'm trying to do but not exactly. I need to sequentially group number the results in an access query as if by grouping...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
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,...
0
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...

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.