T-SQL: Top 2 salaries grouped by Employee ID

I have the following table
Table: SALARY      
Employee ID      Salary
17      130000
17      150000
17      175000
17      140000
18      100000
18      190000
18      175000
19      90000
19      85000
19      78000


I need to       write a query to return the top 2 salaries for each employee. It should look like this
Employee ID      Salary
17        175000
17        150000
18        190000
18        175000
19        90000
19        85000
                              
                                                
Thanks for the help
jknj72Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Use the T-SQL RANK function to rank order all salaries by employee, then in a main query that consumes it (because you can't ORDER BY a rank in the SELECT clause) filter to show only the 1's and 2's.
SELECT EmployeeID, Salary, rank_order
FROM (
   SELECT EmployeeID, salary, RANK() OVER (PARTITION BY EmployeeID ORDER BY Salary DESC) As rank_order
   FROM SALARY ) a
WHERE rank_order < 3
ORDER BY EmployeeID, rank_order

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
virtuadeptCommented:
SELECT emp_id, max(salary)  as salary 
FROM salary as b
INNER JOIN (
SELECT emp_id, max(salary) high_salary
FROM salary 
GROUP BY emp_id
) as a
ON b.emp_id = a.emp_id
WHERE b.salary < a.salary
GROUP BY emp_id
UNION ALL
SELECT emp_id, max(salary) as salary
FROM salary 
GROUP BY emp_id

Open in new window


Something like that. Top select of the union with the inner select achieves getting the 2nd highest salary, and bottom select after union is the same as the inner select of the top one and that is what gets the highest salary. There is probably a more efficient way to do this maybe.
0
PortletPaulfreelancerCommented:
I would recommend the analytic function DENSE_RANK(), so that there is no possibility of gaps in the ranking results. Fundamantally it's the same as jimhorn's suggestion, just change from rank() to dense_rank()

http://technet.microsoft.com/en-us/library/ms173825(v=sql.105).aspx
Returns the rank of rows within the partition of a result set, without any gaps in the ranking. The rank of a row is one plus the number of distinct ranks that come before the row in question.
http://technet.microsoft.com/en-us/library/ms176102(v=sql.105).aspx
Returns the rank of each row within the partition of a result set. The rank of a row is one plus the number of ranks that come before the row in question.
SELECT
        EmployeeID
      , Salary
      , salary_rank
FROM (
        SELECT
                EmployeeID
              , salary
              , DENSE_RANK() OVER (PARTITION BY EmployeeID ORDER BY Salary DESC) AS salary_rank
        FROM SALARY
        ) AS E
WHERE salary_rank < 3
ORDER BY
        EmployeeID
      , salary_rank
;

Open in new window

0
jknj72Author Commented:
Thanks JimHorn
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Thanks for the grade.  Good luck with your project.  -Jim
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.