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: 1784
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: 1784
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: 1784
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 reg query on DYNALLOC feature raghuraman123 SYNCSORT 12 Wed Jan 10, 2018 2:42 pm
No new posts query to fetch record which has only ... maxsubrat DB2 12 Mon Dec 11, 2017 5:03 pm
No new posts Merging 2 records at multiple rows wi... Bijesh DFSORT/ICETOOL 2 Wed Dec 06, 2017 1:50 am
No new posts Can we combine the rows with same key... V S Amarendra Reddy DB2 12 Fri Dec 01, 2017 10:29 pm
No new posts How to query MAXGENS of an existing P... Chuchulo Hatyzak JCL & VSAM 4 Wed Nov 22, 2017 10:23 pm

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