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 > Data Access, Manipulation & Batch Languages > ANSI SQL > limit rows

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-08-11, 16:54
Jazzy88 Jazzy88 is offline
Registered User
 
Join Date: Sep 2011
Posts: 1
limit rows

Hi,

I have a table like:

Name date numb

jan 7/11/11 3
truus 3/10/11 6
pieter 7/09/11 8
sjon 1/02/11 2
truus 4/10/11 0
pieter 7/12/11 1
jan 1/01/11 6
sjon 7/11/11 7

and so on.

I need to limit the rows, from each name the highest 5 dates and then the sum of numb.

please help me

thank you
Reply With Quote
  #2 (permalink)  
Old 09-08-11, 17:26
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Not sure about the "then the sum of numb" part, but if you are looking for a single value per name, then this is probably what you are looking for:
Code:
SELECT name
       sum(numb)
FROM (
    SELECT name,
           date,
           numb,
           row_number() over (partition by name order by date desc) as row_num
    FROM your_table
) t
WHERE row_num <= 5 
GROUP BY name
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