View previous topic :: View next topic
|
Author |
Message |
kregen
New User
Joined: 16 Mar 2006 Posts: 21
|
|
|
|
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 |
|
|
PeterHolland
Global Moderator
Joined: 27 Oct 2009 Posts: 2481 Location: Netherlands, Amstelveen
|
|
Back to top |
|
|
kregen
New User
Joined: 16 Mar 2006 Posts: 21
|
|
|
|
thank you for help, but i don't know how to use it?
is there an example anywhere?
cu
kregen |
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
Well, google seems to think so: db2 sql group by example |
|
Back to top |
|
|
sushanth bobby
Senior Member
Joined: 29 Jul 2008 Posts: 1020 Location: India
|
|
|
|
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 |
|
|
GuyC
Senior Member
Joined: 11 Aug 2009 Posts: 1281 Location: Belgium
|
|
|
|
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 |
|
|
|