Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
obiwaugh
Starting Member
27 Posts |
Posted - 2004-06-17 : 17:18:48
|
| I need to select the top two rows for each employee on a job history table. Data in the table could like this:EMPLID,EFFDT,EFFSEQ,ACTION1001,10/24/04,1,PAY1001,10/24/04,0,XFR1001,10/22/03,0,HIR1002,04/16/04,0,PAY1002,02/14/03,1,PAY1002,02/14/03,0,XFRIf two actions occur on the same day, they are differentiated by the EFFSEQ field. The highest EFFSEQ is the most recent action. I want to return the top 2 rows. Is there a way to rank the rows of data so that each row will have a unique ranking that I can select from?Thanks! |
|
|
Pat Phelan
Posting Yak Master
187 Posts |
Posted - 2004-06-17 : 17:47:24
|
Maybe something like:SELECT EMPLID, EFFDT, EFFSEQ, ACTION FROM PS_JOB AS A WHERE 2 > (SELECT Count(*) FROM PS_JOB AS B WHERE B.EMPLID = A.EMPLID AND (B.EFFDT > A.EFFDT OR (B.EFFDT = A.EFFDT AND B.EFFSEQ > A.EFFSEQ))) ORDER BY EMPLID, EFFDT, EFFSEQ -PatP |
 |
|
|
|
|
|
|
|