Selecting top records [message #606752] |
Tue, 28 January 2014 15:45 |
|
KeithS
Messages: 3 Registered: February 2012
|
Junior Member |
|
|
Hello,
I searched through the Top records post, but did not find an answer and am hopeful someone can provide some help.
I have a table called Progress_Notes. There are 3 fields in it I need to use for a report, note_nbr (primary key), client_nbr, service_date.
The note_nbr is obviously system assigned. Each client could have multiple records. I want to select the 2 note_nbrs for each client_nbr and the service_date associated with the record that would be the last 2 based on the system assigned note_nbr.
Example:
note_nbr client_nbr service_date
10 4441 1/1/2013
11 4441 1/10/2013
12 4441 1/15/2013
13 4441 1/20/2013
14 5552 1/25/2013
15 5552 1/27/2013
16 5552 1/30/2013
I would want the results to be
note_nbr client_nbr service_date
12 4441 1/15/2013
13 4441 1/20/2013
15 5552 1/27/2013
16 5552 1/30/2013
Any help on developing the query to get these results would be of great help.
Thanks.
|
|
|
|
Re: Selecting top records [message #606804 is a reply to message #606754] |
Wed, 29 January 2014 08:43 |
|
KeithS
Messages: 3 Registered: February 2012
|
Junior Member |
|
|
Thank you Littlefoot. I was able to get this to work with your help and the link you provided. Below is my code, if it would help anyone else.
SELECT note_nbr,
client_nbr,
service_date
FROM (SELECT note_nbr,
client_nbr,
service_date,
Rank()
over (
PARTITION BY client_nbr
ORDER BY note_nbr DESC) note_rank
FROM progress_notes)
WHERE note_rank <= 2
*BlackSwan formatted code & added {code} tags. Please do so yourself in the future.
[Updated on: Wed, 29 January 2014 08:58] by Moderator Report message to a moderator
|
|
|
|