how to get second highest salary department wise without using analytical functions?

前端 未结 11 2051
孤独总比滥情好
孤独总比滥情好 2021-01-06 03:08

Suppose we have 3 employees in each department.we have total 3 departments . Below is the sample source table

Emp deptno salary
A    10     1000
B    10              


        
11条回答
  •  青春惊慌失措
    2021-01-06 04:10

    Quite straightforward and declarative, but slow

    select 
      t1.*
    from 
      #tmp t1
      inner join #tmp h1 on h1.dept = t1.dept and h1.emp <> t1.emp
      left outer join #tmp h2 on h2.dept = h1.dept and h2.salary > h1.salary
      left outer join #tmp t2 on t1.dept = t2.dept and t2.salary > t1.salary and t2.emp <> h1.emp
    where 
      t2.emp is null and h2.emp is null
    

提交回复
热议问题