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
 
first n rows for every group

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

New User


Joined: 16 Mar 2006
Posts: 18

PostPosted: Wed Feb 27, 2013 2:44 pm    Post subject: first n rows for every group
Reply with quote

hello,
i need a SQL that fetch the first 500 Rows for every Group.

Example: Table
Code:
 
NBR TEXT 
001 AAAA
002 BBBB
003 AAAA
004 CCCC
005 AAAA
006 BBBB
007 BBBB
008 BBBB
009 BBBB
010 BBBB
011 BBBB
021 CCCC
022 CCCC
023 CCCC
024 CCCC
025 CCCC
026 CCCC
031 DDDD
032 DDDD
033 DDDD


in this example i want the first 5 rows of any Group of TEXT.
when there are not enough rows available. i want all rows that are
available.

Result
Code:
 
NBR TEXT 
001 AAAA
003 AAAA
005 AAAA
002 BBBB
006 BBBB
007 BBBB
008 BBBB
009 BBBB
004 CCCC
021 CCCC
022 CCCC
023 CCCC
024 CCCC
031 DDDD
032 DDDD
033 DDDD


is there nay way to do this?

cu
kregen
Back to top
View user's profile Send private message

PeterHolland

Global Moderator


Joined: 27 Oct 2009
Posts: 2442
Location: Netherlands, Amstelveen

PostPosted: Wed Feb 27, 2013 2:54 pm    Post subject:
Reply with quote

The following link maybe of help :

http://pic.dhe.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=%2Fcom.ibm.db2z10.doc.sqlref%2Fsrc%2Ftpc%2Fdb2z_sql_groupbyclause.htm
Back to top
View user's profile Send private message
kregen

New User


Joined: 16 Mar 2006
Posts: 18

PostPosted: Wed Feb 27, 2013 3:35 pm    Post subject:
Reply with quote

PeterHolland wrote:
The following link maybe of help :

http://pic.dhe.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=%2Fcom.ibm.db2z10.doc.sqlref%2Fsrc%2Ftpc%2Fdb2z_sql_groupbyclause.htm


thank you for help, but i don't know how to use it?

is there an example anywhere?

cu
kregen
Back to top
View user's profile Send private message
Bill Woodger

DFSORT Moderator


Joined: 09 Mar 2011
Posts: 7315

PostPosted: Wed Feb 27, 2013 3:52 pm    Post subject: Reply to: first n rows for every group
Reply with quote

Well, google seems to think so: db2 sql group by example
Back to top
View user's profile Send private message
sushanth bobby

Senior Member


Joined: 29 Jul 2008
Posts: 1013
Location: India

PostPosted: Thu Feb 28, 2013 9:56 am    Post subject:
Reply with quote

Hello kregen,

If you are on DB2 V9, try this
Code:
select * from
(
select    nbr, text,
   dense_rank() over(partition by nbr, text order by nbr) as rank
from table
) as result
where rank <=5


To know more information on functions used, you can google with keywords: DB2 OLAP functions

Most of the examples in the web doesn't have sql in code tags, so it takes little bit of effort to format them in mind and learn them. This PDF you can start with.

Thanks,
Sushanth
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Thu Feb 28, 2013 6:05 pm    Post subject:
Reply with quote

A Solution which probably is more performant :
Code:
select A.NBR, C.TEXT
  from  (select NBR from Table1 group by NBR) A
, table (select   B.text  from Table1 where A.NBR = B.NBR
          order by text
          fetch first 5 rows only)
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 Conditional highlighting of rows in I... packerm CLIST & REXX 2 Thu Sep 21, 2017 6:50 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
This topic is locked: you cannot edit posts or make replies. Limit access to certain RACF group cvnlynn CLIST & REXX 5 Wed Aug 23, 2017 2:28 am
No new posts compare 2 rows within the same file ram_vizag SYNCSORT 7 Wed Jun 14, 2017 12:34 am

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