假设我们每个部门有3名员工。我们共有3个部门。以下是示例源表
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
输出
B 10 2000
F 20 8000
G 30 17000
使用分析函数dense_rank,我们可以明智地获得第二高薪。
我们可以在不使用任何分析函数的情况下实现这一目标吗?
Max()是否也是解析函数?
答案 0 :(得分:4)
这是一种痛苦,但你可以做到。以下查询获得第二高薪:
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;
然后您可以使用它来获得员工:
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;
答案 1 :(得分:2)
这将为您提供每个部门的第二高薪:
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)
group by b.deptno
答案 2 :(得分:0)
相当简单和陈述,但很慢
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
答案 3 :(得分:0)
创建表格并插入虚拟数据
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)
查询以获得结果
;WITH cteRowNum AS (
SELECT *,
DENSE_RANK() OVER(PARTITION BY deptId ORDER BY Sal DESC) AS RowNum
FROM #Employee
)
SELECT *
FROM cteRowNum
WHERE RowNum = 2;
答案 4 :(得分:0)
你可以找到第二高的薪水:
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
并且没有MAX()不是分析函数。
答案 5 :(得分:0)
在MySQL上,鉴于表名是薪水,您如何才能获得第二高的薪水:
通过嵌套查询:(您可以分别将偏移量0/1/2更改为第一,第二和第三位)
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);
或者可以通过创建等级:(您可以分别将rank = 1/2/3更改为第一,第二和第三名)
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;
答案 6 :(得分:0)
使用Correlated Subquery
的解决方案:
SELECT * FROM emp e1 WHERE 2 = (SELECT COUNT(DISTINCT sal)
FROM emp e2
WHERE e1.sal <= e2.sal
AND e1.deptno = e2.deptno
);
答案 7 :(得分:-1)
SQL查询:
select TOP 2 max(salary),Emp from EMployee where deptno='your_detpno'
答案 8 :(得分:-1)
非常简单的逻辑。
请尝试:
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 :(得分:-1)
选择min(薪水),deptno来自 (从表ORDER BY工资DESC中选择不同的前2名工资,deptno) 作为一个 deptno小组
答案 10 :(得分:-2)
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)