This may seem a very simple question, but I am trying to find a way to list say the top 5 values (or less if fewer exist) for each person in a table witout having to resort to cursors.
Here is a simplified version of the problem:
Now, the results I want are:
Two of us have been struggling over this for around 3 hrs and we have the added pressure of a very tight deadline.
Hopefully someone can help before Mr Cursor comes a-callin'!
Here is a simplified version of the problem:
Code:
CREATE TABLE tbl1
(
id int IDENTITY NOT NULL PRIMARY KEY,
PersonCode varchar(6) NOT NULL,
DataValue int
)
go
INSERT tbl1 VALUES ('ABC123, 18')
INSERT tbl1 VALUES ('ABC123, 7')
INSERT tbl1 VALUES ('ABC123, 14')
INSERT tbl1 VALUES ('ABC123, 92')
INSERT tbl1 VALUES ('ABC123, 12')
INSERT tbl1 VALUES ('ABC123, 15')
INSERT tbl1 VALUES ('ABC123, 3')
INSERT tbl1 VALUES ('ABC123, 6')
INSERT tbl1 VALUES ('ABC123, 68')
INSERT tbl1 VALUES ('DEF456, 5')
INSERT tbl1 VALUES ('DEF456, 6')
INSERT tbl1 VALUES ('DEF456, 78')
INSERT tbl1 VALUES ('GHI789, 63')
INSERT tbl1 VALUES ('GHI789, 12')
INSERT tbl1 VALUES ('GHI789, 148')
INSERT tbl1 VALUES ('GHI789, 15')
INSERT tbl1 VALUES ('GHI789, 11')
INSERT tbl1 VALUES ('GHI789, 44')
INSERT tbl1 VALUES ('GHI789, 32')
go
Now, the results I want are:
Code:
PersonCode DataValue
---------- ---------
ABC123 92
ABC123 68
ABC123 18
ABC123 15
ABC123 14
DEF456 78
DEF456 6
DEF456 5
GHI789 148
GHI789 63
GHI789 44
GHI789 32
GHI789 15
Hopefully someone can help before Mr Cursor comes a-callin'!