how to get second highest salary department wise w

2019-04-13 09:00发布

问题:

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     2000
C    10     3000
D    20     7000
E    20     9000
F    20     8000
G    30     17000
H    30     15000
I    30     30000

Output

B    10     2000
F    20     8000
G    30     17000

With using analytic function dense_rank we can achive the second highest salary dept wise.

Can we achieve this without using ANY analytic function ???

Is Max() is also analytic function ??

回答1:

It is a pain, but you can do it. The following query gets the second highest salary:

select t.deptno, max(t.salary) as maxs
from table t
where t.salary < (select max(salary)
                  from table t2
                  where t2.deptno = t.deptno
                 )
group by t.deptno;

You can then use this to get the employee:

select t.*
from table t join
     (select t.deptno, max(t.salary) as maxs
      from table t
      where t.salary < (select max(salary)
                        from table t2
                        where t2.deptno = t.deptno
                       )
      group by t.deptno
     ) tt
     on t.deptno = tt.deptno and t.salary = tt.maxs;


回答2:

This will give you 2nd highest salary in each department:

SELECT Emp, deptno, salary
FROM Emp a
WHERE 1 = (SELECT COUNT(DISTINCT salary) 
        FROM Emp b 
        WHERE b.salary > a.salary AND a.deptno = b.deptno)


回答3:

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


回答4:

Create table and insert dummy data

CREATE TABLE #Employee
(
 Id Int,
 Name NVARCHAR(10), 
 Sal int, 
 deptId int
)


INSERT INTO #Employee VALUES
(1, 'Ashish',1000,1),
(2,'Gayle',3000,1),
(3, 'Salman',2000,2),
(4,'Prem',44000,2)

Query to get result

 ;WITH cteRowNum AS (
SELECT *,
       DENSE_RANK() OVER(PARTITION BY deptId ORDER BY Sal DESC) AS RowNum
    FROM #Employee
 )
 SELECT *
 FROM cteRowNum
 WHERE RowNum = 2;


回答5:

You can find 2nd highest salary something like this:

select max(a.Salary),a.Deptno from Employee a join (select MAX(salary) salary 
from Employee group by Deptno) b on a.Salary < b.salary group by a.Deptno

And no MAX() is not an analytic function.

Reference



回答6:

On MySQL this how you can get second highest salary, given table name is salaries:

By Nested Queries: (where you can change offset 0/1/2 for first, second and third place respectively)

select
  *
from
  salaries as t1 
where 
  t1.salary = (select 
               salary
             from 
               salaries
             where 
             salaries.deptno = t1.deptno ORDER by salary desc limit 1 offset 1);

or might be by creating rank: (where you can change rank= 1/2/3 for first, second and third place respectively)

SET @prev_value = NULL;
SET @rank_count = 0;
select * from 
(SELECT
  s.*, 
  CASE 
      WHEN @prev_value = deptno THEN @rank_count := @rank_count + 1
      WHEN @prev_value := deptno THEN @rank_count := 1 
      ELSE @rank_count := 1 
  END as rank
FROM salaries s
ORDER BY deptno, salary desc) as t
having t.rank = 2;


回答7:

SQL Query:

select TOP 2 max(salary),Emp from EMployee where deptno='your_detpno'


回答8:

Very simple logic.

Please try:

SELECT dept as dd, ( SELECT ee.salary FROM `employees` as ee WHERE ee.dept=dd 
ORDER BY ee.salary DESC LIMIT 1,1 ) as sndHigh 
FROM `employees` 
WHERE 1 
GROUP BY dept


回答9:

select min(salary),deptno from (SELECT distinct top 2 salary,deptno from table ORDER BY salary DESC) as a group by deptno



回答10:

CREATE TABLE Employee
    ([Name] varchar(1), [Dept] varchar(1), [Salary] int)
;

INSERT INTO Employee
    ([Name], [Dept], [Salary])
VALUES
    ('a', 'M', 20),
    ('b', 'M', 25),
    ('c', 'M', 30),
    ('d', 'C', 44),
    ('e', 'C', 45),
    ('f', 'C', 46),
    ('g', 'H', 20)