2010-01-29 82 views

回答

11

試着這麼做:

SELECT TOP 1 compensation FROM (
    SELECT TOP 2 compensation FROM employees 
    ORDER BY compensation DESC 
) AS em ORDER BY compensation ASC 

本質:

  • 查找降序排列的前2薪水。
  • 其中2,找到最高薪水的升序。
  • 選定的值是第二高的工資。

如果薪資不明顯,則可以使用SELECT DISTINCT TOP ...代替。

+1

當TOP 1薪酬由超過1名員工賺取時,此查詢將返回錯誤結果。 – 2010-01-29 08:24:35

+1

如果工資不明顯,您可以使用'SELECT DISTINCT TOP ...'來代替。 – 2010-01-29 08:42:43

4

試試這個:

SELECT 
    salary, 
    employeeid 
FROM 
    employees 
ORDER BY 
    salary DESC 
LIMIT 2 

然後就得到了第二排。

+0

如果前兩名工資相同,該怎麼辦? – moghya 2018-02-28 18:02:50

9

也許你應該使用DENSE_RANK

SELECT * 
FROM (
     SELECT 
     [Salary], 
     (DENSE_RANK() 
     OVER 
     (
      ORDER BY [Salary] DESC)) AS rnk 
     FROM [Table1] 
     GROUP BY [Num] 
    ) AS A 
WHERE A.rnk = 2 
+3

用DENSE_RANK()替換RANK(),它起作用。 – 2010-01-29 08:27:52

+0

是的弗蘭克,你是對的 – CoderHawk 2010-01-29 09:03:46

+0

我已經改變了相應的查詢 – CoderHawk 2010-01-29 09:52:26

47

試試這個:

SELECT max(salary) 
FROM emptable 
WHERE salary < (SELECT max(salary) 
       FROM emptable); 
3
select distinct(t1.sal) 
from emp t1 
where &n=(select count(distinct(t2.sal)) from emp t2 where t1.sal<=t2.sal); 

輸出: 對於n輸入值:如果你想第二高,進入2;如果你想5,輸入n = 3

2
select * from compensation where Salary = (
     select top 1 Salary from (
     select top 2 Salary from compensation 
     group by Salary order by Salary desc) top2 
     order by Salary) 

,這將給你所有行與第二高的薪水,這幾個人可以共享

2
select max(Salary) from Employee 
where Salary 
not in (Select Max(Salary) from Employee) 
2
select max(Salary) from Employee 
where Salary 
    not in (Select top4 salary from Employee); 

因爲答案如下

MAX(5,6,7,8)

所以第五最高紀錄是DISPLA YED,前四將不被視爲

2

試試這個:

select max(Emp_Sal) 
from Employee a 
where 1 = (select count(*) 
     from Employee b 
     where b.Emp_Sal > a.Emp_Sal) 
+0

如果有重複的工資,這不起作用。比方說,有三個相同的最高工資30000,你發現第二高。 – 2018-01-23 05:18:50

3

要找到員工的第二個最高的工資,

SELECT MAX(salary) FROM employee 
WHERE salary NOT IN (
    SELECT MAX (salary) FROM employee 
) 

要找到員工的第一和第二最高的薪水,

SELECT salary FROM (
    SELECT DISTINCT(salary) FROM employee ORDER BY salary DESC 
) WHERE rownum<=2 

此查詢工作正常,因爲我用

+0

其實,如果你有一個表: 1. 100 2. 100 3. 50 您的查詢將返回** **空,這是在這種情況下,不正確的答案 – kvatashydze 2016-10-17 11:17:17

2
SELECT 
    TOP 1 salary 
FROM 
    (
     SELECT 
      TOP 2 salary 
     FROM 
      employees 
    ) sal 
ORDER BY 
    salary DESC; 
2
select max(sal) from emp 
where sal not in (select max(sal) from emp) 

OR

select max(salary) from emp table 
where sal<(select max(salary)from emp) 
9

大多數其他的答案似乎是分貝具體。

一般的SQL查詢應該如下:

select sal from emp a where N = (select count(distinct sal) from emp b where a.sal <= b.sal) 
where N = any value. 

與此查詢應該能夠在任何數據庫上運行。

+0

出色答卷+1 – anubhava 2016-11-19 15:00:55

5
select max(Emp_Sal) 
from Employee a 
where 1 = (select count(*) 
     from Employee b 
     where b.Emp_Sal > a.Emp_Sal) 

是跑步的人。

4

//要選擇的員工的名字,其薪水第二高的

SELECT name 
FROM employee WHERE salary = 
     (SELECT MIN(salary) FROM 
      (SELECT TOP (2) salary 
       FROM employee 
       ORDER BY salary DESC)) 
33

簡單的答案:

SELECT sal 
FROM emp 
ORDER BY sal DESC 
LIMIT 1, 1; 

將只得到第二最高的薪水。

如果您需要任何第3或第4或第N個值,則可以增加第一個值,然後再加上LIMIT (n-1)即。對於第四工資:LIMIT 3, 1;

+1

這要工作如果你有兩個薪水相同的記錄。它想給你第二最大。 – 2016-06-02 12:13:17

+1

@ mukeshpatel不同需要應用感謝指出。 – SandeepGodara 2016-06-13 13:14:47

1
select * from emp 
where sal=(select min(sal) from 
(select sal from(select distinct sal from emp order by sal desc) 
where rownum<=n)); 

n可以是你想看到你可以看到有n個最高工資誰的那個人* 強大的所有文本字段中的值......

*

相關問題