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
 

 

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: 2422
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: 7251

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: 1278
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 DB2 query Using Recursion, Converting... smilewithashu2 DB2 1 Tue Jan 03, 2017 12:50 pm
No new posts Syncsort Help to group fields sudhakarraju SYNCSORT 6 Thu Dec 29, 2016 1:38 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
No new posts Row-Numbers of distinct rows? Auryn DB2 1 Thu Oct 20, 2016 4:38 pm
No new posts abend sort based on count records in ... anatol DFSORT/ICETOOL 5 Mon Oct 17, 2016 10:10 pm


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