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

Help!! Give row numbers to rows returned in DB2 query


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

New User


Joined: 20 Jan 2007
Posts: 2
Location: India

PostPosted: Fri Feb 03, 2012 8:50 pm
Reply with quote

Hi,

I want to assign numbers to each row retrieved from the query. If i would say select row number, emp id, emp name from employees I would want db2 to return

1 626 John
2 678 David
3 999 Brad

is there a way I could do this in DB2

Thanks much!
Back to top
View user's profile Send private message
Akatsukami

Global Moderator


Joined: 03 Oct 2009
Posts: 1788
Location: Bloomington, IL

PostPosted: Fri Feb 03, 2012 9:27 pm
Reply with quote

What version of DB2 do you have installed?
Back to top
View user's profile Send private message
jedidiah_raj

New User


Joined: 20 Jan 2007
Posts: 2
Location: India

PostPosted: Fri Feb 03, 2012 11:16 pm
Reply with quote

Akatsukami wrote:
What version of DB2 do you have installed?

9
Back to top
View user's profile Send private message
Akatsukami

Global Moderator


Joined: 03 Oct 2009
Posts: 1788
Location: Bloomington, IL

PostPosted: Sat Feb 04, 2012 12:04 am
Reply with quote

Is a column in a table which will be included in your SELECT statemeny given the data type ROWID?
Back to top
View user's profile Send private message
Akatsukami

Global Moderator


Joined: 03 Oct 2009
Posts: 1788
Location: Bloomington, IL

PostPosted: Sat Feb 04, 2012 12:36 am
Reply with quote

Addendum: if not, and if the row number can be volatile across REORGs, you can use:
Code:
SELECT RID(EMPLOYEES) AS ROW_NUMBER, EMP_ID, EMP_NAME FROM EMPLOYEES
Back to top
View user's profile Send private message
agkshirsagar

Active Member


Joined: 27 Feb 2007
Posts: 691
Location: Earth

PostPosted: Sat Feb 04, 2012 2:46 am
Reply with quote

@OP,
If I understood your question correctly, this should do the trick for you.

Code:
SELECT ROW_NUMBER() OVER(ORDER BY EMP_ID) AS ROW_NUM,
EMP_ID,
EMP_NAME FROM EMPLOYEES;

IMHO, this trick has no practical value in application programming. Was this an interview question?
Back to top
View user's profile Send private message
karthikr44

Active User


Joined: 25 Aug 2007
Posts: 235
Location: Chennai

PostPosted: Mon Feb 06, 2012 1:47 pm
Reply with quote

HI,

You can also use CREATE SEQUENCE feauture..

Please see the below example...

Code:

CREATE SEQUENCE MYSEQ   
    START WITH 1         
    INCREMENT BY 1       
    NO MAXVALUE         
    NO CYCLE             
    CACHE 24;           

SELECT NEXT VALUE FOR MYSEQ AS SEQ,
row number, emp id, emp name
from employees I;


Try it..

Regards
R KARTHIK
Back to top
View user's profile Send private message
agkshirsagar

Active Member


Joined: 27 Feb 2007
Posts: 691
Location: Earth

PostPosted: Wed Feb 08, 2012 6:32 am
Reply with quote

Karthik's solution removes the ORDER BY overhead present in my query.

If the order is not important, please use-

Code:
SELECT ROW_NUMBER() OVER() AS ROW_NUM,
EMP_ID,
EMP_NAME FROM EMPLOYEES;
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 Extracting Variable decimal numbers f... DFSORT/ICETOOL 17
No new posts To get the count of rows for every 1 ... DB2 3
No new posts Generate random number from range of ... COBOL Programming 3
No new posts RC query -Time column CA Products 3
No new posts Exclude rows with > than x occurre... DFSORT/ICETOOL 6
Search our Forums:

Back to Top