-1

我试图从这样的前 5 名员工那里获得所有 3 最高薪水

    salary
  35000
  34000
  20000
  12000
  500
  40000
  25000
  41000
  90000
  550000

询问

select top 5
  (SELECT MAX(grosssalary) FROM Detail) maxsalary ,
   (SELECT MAX(grosssalary) FROM Detail) sec_max_salary,
  (SELECT MAX(grosssalary) FROM Detail
  WHERE grosssalary NOT IN (SELECT MAX(grosssalary) FROM Detail )) as third_max_salary

但这显示了这样的数据

maxsalary   sec_max_salary  third_max_salary
550000       550000            41000

我想要这样的数据在哪里

   maxsalary    sec_max_salary  third_max_salary
    550000       90000            41000
4

10 回答 10

1

执行 aCTEROWNUMBER()salary DESC外部查询中获取行号等于 3 的记录。

;WITH CTE AS
(
    SELECT RN = ROW_NUMBER() OVER (ORDER BY salary DESC),
           Salary
     FROM [YourTable]
 )

 SELECT Salary
 FROM CTE
 WHERE RN <= 3

注意:如果你想要第三高的薪水RN=3,如果你想要所有前三名的薪水,那么使用RN<=3

如果你想要前 3 名的最高薪水,那么你也可以这样做:

SELECT TOP 3 Salary
FROM [YourTable]
ORDER BY Salary DESC
于 2016-12-05T06:00:36.853 回答
1
 CREATE TABLE #A
 (

 salary INT
 )
 INSERT INTO #A VALUES
  (35000),
  (34000),
  (20000),
  (12000),
  (500  ),
  (40000),
  (25000),
  (41000),
  (90000),
  (550000)


select [1] maxsalary, [2] sec_max_salary, [3] third_max_salary
from (
SELECT *
FROM (

    SELECT *,
           RN = ROW_NUMBER() OVER (ORDER BY Salary DESC)
    FROM #A
)a
WHERE RN  <= 3

) src
pivot
(
  MAX(SALARY)
  for RN in ([1], [2], [3])
) piv;

输出

maxsalary   sec_max_salary  third_max_salary
550000       90000           41000
于 2016-12-05T06:08:04.670 回答
1

表名称:-ThreeHighestSalaries

姓名--------工资

  • e5 -------- 45000
  • e3 -------- 30000
  • e2 -------- 49000
  • e4 -------- 36600
  • e1 -------- 58000
  • e6 -------- 58000

应该返回 58000,49000,45000。

1) 从 ThreeHighestSalaries 中选择 *,dense_rank() over(order by Salary desc) 作为薪级

上面的查询返回:

姓名 --------- 薪水------职级

  • e1 --------- 58000 --------- 1
  • e6 --------- 58000 --------- 1
  • e2 --------- 49000 --------- 2
  • e5 --------- 45000 --------- 3
  • e4 --------- 36600 --------- 4
  • e3 --------- 30000 --------- 5

2) 从 (select *,dense_rank() over(order by Salary desc) assalaryrank from ThreeHighestSalaries) 中选择不同的薪水作为 temp,其中 temp.salaryrank<=3 按薪水 desc 排序

上述查询返回结果为:

薪水

  • 58000
  • 49000
  • 45000
于 2018-07-27T12:29:27.270 回答
0

使用DENSE_RANK()对薪水进行排序,然后使用CASE表达式查找前 3 名薪水。

询问

;with cte as(
    select [sal_rank] = dense_rank() over(
        order by [grosssalary] desc
    ), *
    from [your_table_name]
)
select max(case [sal_rank] when 1 then [grosssalary] end) as [maxsalary],
max(case [sal_rank] when 2 then [grosssalary] end) as [sec_max_salary],
max(case [sal_rank] when 3 then [grosssalary] end) as [third_max_salary]
from cte;
于 2016-12-05T06:07:33.167 回答
0

如果您正在使用,请SQL SERVER 2012+使用OFFSET

SELECT TOP 1 salary,
             (SELECT salary FROM Yourtable
              ORDER  BY salary DESC OFFSET 1 ROWS FETCH NEXT 1 ROWS ONLY),
             (SELECT salary FROM Yourtable 
              ORDER  BY salary DESC OFFSET 2 ROWS FETCH NEXT 1 ROWS ONLY)
FROM   Yourtable
ORDER  BY salary DESC 

如果你只想要第三高的薪水,那么

SELECT salary
FROM   yourtable
ORDER  BY salary DESC 
OFFSET 2 ROWS FETCH NEXT 1 ROWS ONLY
于 2016-12-05T06:22:42.597 回答
0

下面的代码将引导您获得第 N 个最大或最小的工资值,而与数据类型无关,即。整数或 Vearchar。只需将“<”符号更改为较小的第 n 个值。

select * from txt_salary as t1 where 'nth largest' =
(select count(salary) from txt_salary as t2 where t1.salary<=t2.salary)
于 2017-08-02T10:31:22.417 回答
0

对于 SQL 服务器

SELECT e1.* FROM empe1 WHERE e1.salary = ( select MIN(e2.salary) from (select top 3 DISTINCT(e3.salary) from empas e3 ORDER by e3.salary desc ) as e2

对于 MYSQL

SELECT e1.* FROM empe1 WHERE e1.salary = ( select MIN(e2.salary) from (select DISTINCT(e3.salary) from empas e3 ORDER by e3.salary desc limit 0,3) as e2 )

于 2017-11-24T07:33:02.547 回答
0

从员工中选择 max(salary) 作为最高
UNION从员工
中选择 max(salary) 作为 2nd_highest,其中薪水 <(从员工中选择 max(salary))
UNION
max(salary) 作为员工中的 3rd_highest,其中薪水 <(从员工中选择 max(salary)员工工资<(从员工中选择最大(工资)))=

对于第 18 高薪水 =
选择薪水、ename、eid FROM(选择 e.ename、e.salary、e.eid、rank() OVER(按 e.eid desc 排序)排名)WHERE rank=18

于 2018-03-01T07:04:23.027 回答
0

声明@tem1 表(salary int)

插入@tem1(salary) 从 Detail order by Grosssalary desc 中选择前 3 个 Grosssalary

从@tem1 order by sal asc 中选择 top 1 sal 作为 3rdsalary

于 2018-09-10T05:18:00.770 回答
-2
select * 
from 
( select *
  from table 
  order by salary desc
  limit 3 
) as top_three
order by top_three.salary asc
limit 1
于 2016-12-05T06:38:52.770 回答