If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > TopInAGroup

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-22-10, 14:10
asburym1 asburym1 is offline
Registered User
 
Join Date: Mar 2010
Posts: 14
Cool TopInAGroup

From the following created table #customer_tbl:

CREATE TABLE #customer_tbl(Col1 datetime, Col2 int, Col3 int, Col4 char(2))

INSERT INTO #customer_tbl Values('2003-07-05', 100131, 715,'AA')
INSERT INTO #customer_tbl Values('2001-03-02', 100142, 700,'AA')
INSERT INTO #customer_tbl Values('2004-09-02', 100154, 38,'AA')
INSERT INTO #customer_tbl Values('2005-08-02', 100113, 73,'AA')
INSERT INTO #customer_tbl Values('2005-08-03', 100234, 78,'BF')
INSERT INTO #customer_tbl Values('2002-01-02', 100357, 706,'BF')
INSERT INTO #customer_tbl Values('2003-01-02', 100052, 706,'BF')
INSERT INTO #customer_tbl Values('2004-01-02', 100291, 706,'BF')
INSERT INTO #customer_tbl Values('2004-06-09', 100317, 390,'DT')
INSERT INTO #customer_tbl Values('2004-06-09', 100139, 700,'DT')
INSERT INTO #customer_tbl Values('2005-08-03', 100154, 38,'DT')
INSERT INTO #customer_tbl Values('2006-07-04', 100113, 73,'DT')
INSERT INTO #customer_tbl Values('2007-03-03', 100234, 78,'KC')
INSERT INTO #customer_tbl Values('2003-01-02', 100348, 706,'KC')
INSERT INTO #customer_tbl Values('2004-11-12', 100556, 706,'KC')
INSERT INTO #customer_tbl Values('2005-01-04', 100741, 706,'KC')

How can I extract the top 2 rows of each Col4 type?

For example:

'2003-07-05', 100131, 715,'AA'
'2001-03-02', 100142, 700,'AA'
'2005-08-03', 100234, 78,'BF'
'2002-01-02', 100357, 706,'BF'
'2004-06-09', 100317, 390,'DT'
'2004-06-09', 100139, 700,'DT'
'2007-03-03', 100234, 78,'KC'
'2003-01-02', 100348, 706,'KC'
Reply With Quote
  #2 (permalink)  
Old 09-22-10, 14:59
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
I think something like:

Code:
select * from customer_tbl where row_number() over (partition by col4 order by col3) <= 2
Andy
Reply With Quote
  #3 (permalink)  
Old 09-22-10, 15:36
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by asburym1 View Post

How can I extract the top 2 rows of each Col4 type?
How do you define "top two" out of many?
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On