select top 1 salary from each department RRS feed

  • Question

  • hi

    i have table1

    id      dep       salary

    i want to know empid from each department that has highest salary in 1 statement.

    just top 1

    hwo to do that

    Monday, May 20, 2013 11:32 PM


  • ;With cte As
    (Select id, dep, salary
      Row_Number() Over(Partition By dep Order By salary Desc) as rn
    From table1)
    Select id, dep
    From cte
    Where rn = 1
    Order By dep;

    • Proposed as answer by SQLZealots Tuesday, May 21, 2013 12:14 AM
    • Marked as answer by Allen Li - MSFT Wednesday, May 29, 2013 9:51 AM
    Monday, May 20, 2013 11:47 PM