Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist 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: 1777
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: 1777
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: 1777
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: 686
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: 686
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 HEX value search in a DB2 query maxsubrat DB2 2 Wed Oct 04, 2017 3:04 pm
No new posts Conditional highlighting of rows in I... packerm CLIST & REXX 2 Thu Sep 21, 2017 6:50 pm
No new posts Create procedure issues -628 when add... chandraBE DB2 1 Mon Sep 18, 2017 12:16 pm
No new posts column with count of rows within dist... ronald wouterson DB2 4 Sun Sep 17, 2017 9:48 pm
No new posts Convert rows and column into JSON for... Dinesh Manivannan DB2 2 Sun Sep 03, 2017 6:50 pm

Facebook
Back to Top
 
Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us