Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

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

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
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    Post subject: Help!! Give row numbers to rows returned in DB2 query
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: 1746
Location: Bloomington, IL

PostPosted: Fri Feb 03, 2012 9:27 pm    Post subject:
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    Post subject:
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: 1746
Location: Bloomington, IL

PostPosted: Sat Feb 04, 2012 12:04 am    Post subject:
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: 1746
Location: Bloomington, IL

PostPosted: Sat Feb 04, 2012 12:36 am    Post subject:
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: 685
Location: Earth

PostPosted: Sat Feb 04, 2012 2:46 am    Post subject:
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    Post subject: Reply to: Help!! Give row numbers to rows returned in DB2 qu
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: 685
Location: Earth

PostPosted: Wed Feb 08, 2012 6:32 am    Post subject:
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    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Join in SQL Query vickey_dw DB2 0 Tue Jan 17, 2017 12:15 am
No new posts Incorrect output when trying to add n... monica1 PL/I & Assembler 7 Fri Jan 13, 2017 5:02 pm
This topic is locked: you cannot edit posts or make replies. MQ setup query mohitsaini Java & MQSeries 4 Sun Jan 08, 2017 1:53 am
No new posts DB2 query Using Recursion, Converting... smilewithashu2 DB2 1 Tue Jan 03, 2017 12:50 pm
No new posts SQL query to run through list of valu... Ni3-db2 DB2 14 Wed Dec 14, 2016 9:52 am


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us