IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

first n rows for every group


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
kregen

New User


Joined: 16 Mar 2006
Posts: 21

PostPosted: Wed Feb 27, 2013 2:44 pm
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: 2481
Location: Netherlands, Amstelveen

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

The following link maybe of help :

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: 21

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

PeterHolland wrote:
The following link maybe of help :

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

Moderator Emeritus


Joined: 09 Mar 2011
Posts: 7309
Location: Inside the Matrix

PostPosted: Wed Feb 27, 2013 3:52 pm
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: 1020
Location: India

PostPosted: Thu Feb 28, 2013 9:56 am
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
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 View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts To get the count of rows for every 1 ... DB2 3
No new posts Exclude rows with > than x occurre... DFSORT/ICETOOL 6
No new posts Convert single row multi cols to sing... DFSORT/ICETOOL 6
No new posts Compare latest 2 rows of a table usin... DB2 1
No new posts How to compare two rows of same table DB2 11
Search our Forums:

Back to Top