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
 

 

SQL Query to achieve control break in result set

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
prasun dhara

New User


Joined: 01 Mar 2008
Posts: 49
Location: kolkata

PostPosted: Tue May 24, 2011 12:43 pm    Post subject: SQL Query to achieve control break in result set
Reply with quote

Hi ,

I have a requirement like below :
Table A
Emp Id Role
1001 x
1001 y
1001 z
1002 c
1002 x
1003 a

I have to write a SQL query on this table to get a result like below

Counter Emp id Role
1 1001 x
2 1001 y
3 1001 z
1 1002 c
2 1002 x
1 1003 a


So I have to add a counter filed in the result set which will increament by one if the Emp id is same with previous one and the counter will re-set if the emp id changes.

Please note I have to do this by SQL query only and I am not allowed to write any COBOL logic

Thanks
Prasun
Back to top
View user's profile Send private message

GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1278
Location: Belgium

PostPosted: Tue May 24, 2011 4:48 pm    Post subject:
Reply with quote

homework!?
I suggest you look up OLAP-function in a SQL-book.
=> row_number() & partition by
Back to top
View user's profile Send private message
prasun dhara

New User


Joined: 01 Mar 2008
Posts: 49
Location: kolkata

PostPosted: Tue May 24, 2011 5:21 pm    Post subject: Reply to: SQL Query to achieve control break in result set
Reply with quote

It's Not home work. It's a project work icon_biggrin.gif

I found a query with
"row_number() & partition by" nut not working


Are these (row_number() & partition by) supported for Db2 zOS v8 ?

This is part of a performance tuning project. we have to implement this logic in SPUFI instead of COBOL

Thanks for your help
Back to top
View user's profile Send private message
Pjoe

New User


Joined: 22 Jul 2005
Posts: 17

PostPosted: Tue May 24, 2011 7:04 pm    Post subject: Reply to: SQL Query to achieve control break in result set
Reply with quote

row_number is not available in DB2 v8 for z /os.
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Tue May 24, 2011 7:06 pm    Post subject:
Reply with quote

why do you need these sequence numbers?
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1278
Location: Belgium

PostPosted: Tue May 24, 2011 7:32 pm    Post subject:
Reply with quote

trust me, implementing these sequence number in an sql pre v9 isn't performant :
Code:
SELECT
   (SELECT COUNT(*)
     FROM TableA s2
    WHERE s2.emp_id = s1.emp_id
      and s2.role <= s1.role) AS counter
   ,emp_id
   ,role
FROM TableA s1
ORDER BY s1.emp_id,role
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 2 Tue Jan 17, 2017 12:15 am
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
This topic is locked: you cannot edit posts or make replies. Db2 SQL query to convert rows into co... subratarec DB2 5 Thu Nov 17, 2016 4:51 pm


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