473,385 Members | 1,593 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,385 software developers and data experts.

Ordinal record number

Dan
How can i find out what record number in the return set I am at?

Example select *,recnum() from foo I would like to see recnum() be 1
then 2,3,4,5 and so on for each record returned.
Nov 12 '05 #1
5 6091
You can add a column to the table using a SEQUENCE or IDENTITY value.
Or, use the rownum function:

SELECT * FROM (SELECT NAME, rownumber() OVER
(ORDER BY NAME)
AS rn FROM ADDRESS)
AS tr
Dan wrote:
How can i find out what record number in the return set I am at?

Example select *,recnum() from foo I would like to see recnum() be 1
then 2,3,4,5 and so on for each record returned.


Nov 12 '05 #2
On 2004-04-13, Dan scribbled:
How can i find out what record number in the return set I am at?

Example select *,recnum() from foo I would like to see recnum() be 1
then 2,3,4,5 and so on for each record returned.


Hi Dan,

AFAIK, you can't directly. Records aren't "numbered" in a relational
database as they are fundamentally unordered. According to relational
theory which is more or less the basis for modern relational databases,
a table (or "relation" in relational theory) is a *set* of tuples where
the word "set" is used in its strict mathematical sense to mean "a
distinct, unordered set of values". Therefore a record can't have a
number.

It would be possible to implement this using a stored procedure which
incremented a counter and included it within the result set it
returned, but you can't implement a function like the RECNUM() function
you describe (I think?). However, I've usually found that what people
wishing to do this /actually/ want to do is /rank/ records. This can be
done using the OLAP functions RANK() or DENSE_RANK() in DB2 (at least
in v7 and upwards - not sure about prior versions).

For example, given the following table of data:

SALES
--------+-------+-------
COUNTRY | MONTH | AMOUNT
--------+-------+-------
GB | 1 | 50000
GB | 2 | 49000
GB | 3 | 53000
FR | 1 | 51000
FR | 2 | 49000
FR | 3 | 52000
DE | 1 | 48000
DE | 2 | 47000
DE | 3 | 51000
--------+-------+-------

You could use the following SQL to rank sales in month 1 by country:

SELECT
RANK() OVER (ORDER BY AMOUNT DESC) AS RANK,
COUNTRY,
AMOUNT
FROM SALES
WHERE MONTH = 1

Which returns the following result:

RANK COUNTRY AMOUNT
---- ------- ------
1 FR 51000
2 GB 50000
3 DE 48000

However, it should be noted that ranking is *not* the same as record
numbering. For example, in month 2 both GB and FR have the same value
in the AMOUNT field. What happens when the above query is executed for
month 2?

RANK COUNTRY AMOUNT
---- ------- ------
1 GB 49000
1 FR 49000
3 DE 47000

Note that the RANK() function assigns the same rank to GB and FR (which
is correct), and then skips a rank before assigning 3 to the DE row. If
you don't want it to skip a rank, use the DENSE_RANK() function instead
which would produce the same ranking for GB and FR, but would assign 2
to the DE row instead.

For more information on the OLAP functions in DB2, read the "Language
Elements" chapter in the SQL Reference. Under the "Expressions" section
you should find the "OLAP functions" section.

Anyway, sorry it's probably not the exact answer you're looking for but
hopefully its given you some ideas to play with.

HTH,
Dave.
--
Dave
Remove "_nospam" for valid e-mail address

"Never underestimate the bandwidth of a station wagon full of CDs doing
a ton down the highway" -- Anon.
Nov 12 '05 #3
On 2004-04-13, Blair Adamache scribbled:
You can add a column to the table using a SEQUENCE or IDENTITY value.
Or, use the rownum function:

SELECT * FROM (SELECT NAME, rownumber() OVER
(ORDER BY NAME)
AS rn FROM ADDRESS)
AS tr
Dan wrote:
How can i find out what record number in the return set I am at?

Example select *,recnum() from foo I would like to see recnum() be
1 then 2,3,4,5 and so on for each record returned.


Damn! How did I miss that? Time to go and RRTFM :-) Still, guess I was
right on one point - a result set requires an explicit order before it
can be numbered in any fashion.

Dave.

--
Dave
Remove "_nospam" for valid e-mail address

"Never underestimate the bandwidth of a station wagon full of CDs doing
a ton down the highway" -- Anon.
Nov 12 '05 #4
Dave Hughes wrote:
On 2004-04-13, Blair Adamache scribbled:
You can add a column to the table using a SEQUENCE or IDENTITY value.
Or, use the rownum function:

SELECT * FROM (SELECT NAME, rownumber() OVER
(ORDER BY NAME)
AS rn FROM ADDRESS)
AS tr

Damn! How did I miss that? Time to go and RRTFM :-) Still, guess I was
right on one point - a result set requires an explicit order before it
can be numbered in any fashion.


You can omit the ORDER BY:

SELECT name, rownumber() over() AS rn
FROM ...

Then you get any arbitrary order from DB2, which might even change from
execution to execution of the statement.

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #5
On 2004-04-14, Knut Stolze scribbled:
Dave Hughes wrote:
On 2004-04-13, Blair Adamache scribbled:
You can add a column to the table using a SEQUENCE or IDENTITY value. >> Or, use the rownum function:
SELECT * FROM (SELECT NAME, rownumber() OVER
(ORDER BY NAME)
AS rn FROM ADDRESS)
AS tr

Damn! How did I miss that? Time to go and RRTFM :-) Still, guess I
was right on one point - a result set requires an explicit order
before it can be numbered in any fashion.


You can omit the ORDER BY:

SELECT name, rownumber() over() AS rn
FROM ...

Then you get any arbitrary order from DB2, which might even change
from execution to execution of the statement.


Argh! Just checked and you're absolutely right, Knut - apologies Dan!
Only RANK and DENSE_RANK require an ORDER BY in the OVER clause.

--
Dave
Remove "_nospam" for valid e-mail address

"Never underestimate the bandwidth of a station wagon full of CDs doing
a ton down the highway" -- Anon.
Nov 12 '05 #6

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

Similar topics

4
by: Terry Coccoli | last post by:
Has anyone created a user function to return the ordinal position based on numeric input? In other words, f(1) returns 'st', f(2) returns 'nd', f(3) returns 'rd', etc.
2
by: CJM | last post by:
I'm running a stored procedure that inserts a record into a table then returns the Identity field value. I've done this kind of thing plenty of times in the past, but I'm obviously doing something...
12
by: Joseph Numpty | last post by:
Hello everyone. My first post... I'd like to add an automatically updating date field to a webpage. I found the below example on the internet which works brilliantly except I'd like an ordinal...
2
by: Mario Pflucker | last post by:
Hi, there Is there a function to obtain the ordinal of a number? i.e. "first", "second", etc Thanks
24
by: Donald Grove | last post by:
I want to populate an array with values from an ado recordset (multiple rows) I use the absolute position of the cursor in the recordset to define the row of my array to be populated. I have a...
4
by: Mark Tarver | last post by:
Prompted by a post on Catalan numbers in Qilang, I got into looking at ordinal numbers as defined by John von Neumann - see http://en.wikipedia.org/wiki/Ordinal_numbers 0 = {} 1 = {0} = {{}} 2...
4
by: yhebib | last post by:
Hello All, I've been browsing and reading all articles I could find on technet ,msdn and other knowledgeable sources to understand the issue I'm dealing with. However, I did not find so far how...
7
by: Dylan Parry | last post by:
Hi folks, I was wondering if there is any way of formatting a date to include the ordinal characters? I've looked at the documentation for DateTime.ToString(), but no where can I find...
3
by: John | last post by:
Hi I am getting the System.IndexOutOfRangeException was unhandled error on the last line of below code; Cmd = New OleDb.OleDbCommand("SELECT FROM ", LocalConn) Reader = Cmd.ExecuteReader()...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.