sql >> Database teknologi >  >> RDS >> Sqlserver

Hvordan finder man den tredje eller nᵗʰ maksimale løn fra løntabellen?

Rækkenummer :

SELECT Salary,EmpName
FROM
  (
   SELECT Salary,EmpName,ROW_NUMBER() OVER(ORDER BY Salary) As RowNum
   FROM EMPLOYEE
   ) As A
WHERE A.RowNum IN (2,3)

Underforespørgsel:

SELECT *
FROM Employee Emp1
WHERE (N-1) = (
               SELECT COUNT(DISTINCT(Emp2.Salary))
               FROM Employee Emp2
               WHERE Emp2.Salary > Emp1.Salary
               )

Topsøgeord :

SELECT TOP 1 salary
FROM (
      SELECT DISTINCT TOP n salary
      FROM employee
      ORDER BY salary DESC
      ) a
ORDER BY salary


  1. 1064 fejl i CREATE TABLE ... TYPE=MYISAM

  2. MySQL - Hvordan unpivot kolonner til rækker?

  3. Fremskynd bulkinsert-operationer med NHibernate

  4. MySQLi count(*) returnerer altid 1