Answers:
使用ROW_NUMBER
(如果要一个)或DENSE_RANK
(对于所有相关行):
WITH CTE AS
(
SELECT EmpID, EmpName, EmpSalary,
RN = ROW_NUMBER() OVER (ORDER BY EmpSalary DESC)
FROM dbo.Salary
)
SELECT EmpID, EmpName, EmpSalary
FROM CTE
WHERE RN = @NthRow
EmpSalary
列上没有索引。另外,相比之下减少了什么?该ROW_NUMBER
方法的优点是可以使用..OVER(PARTITION BY GroupColumn OrderBy OrderColumn)
。因此,您可以使用它来获取组,但仍可以访问它的任何列。
行号:
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)
子查询:
SELECT *
FROM Employee Emp1
WHERE (N-1) = (
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary
)
热门关键字:
SELECT TOP 1 salary
FROM (
SELECT DISTINCT TOP n salary
FROM employee
ORDER BY salary DESC
) a
ORDER BY salary
... WHERE (N-1) = (Subquery)...
工作原理很重要。子查询是一个相关查询,因为其WHERE
子句Emp1
从主查询中使用。每次主查询扫描一行时都会评估子查询。例如,如果我们要从(800,1000,700,750)中找到第三大薪水(N = 3),则第一行的子查询将为SELECT COUNT(DISTINCT(Emp2.Salary)) FROM Employee Emp2 WHERE Emp2.Salary > 800
0。对于第四薪水值(750)... WHERE Emp2.Salary > 750
将为2,即N -1,因此将返回此行。
如果要使用TOP
关键字优化方法,那么第n个最高和最低薪水查询如下,但通过使用聚合函数名称,查询看起来像一个棘手的逆序查询:
N最高工资:
SELECT MIN(EmpSalary)
FROM Salary
WHERE EmpSalary IN(SELECT TOP N EmpSalary FROM Salary ORDER BY EmpSalary DESC)
例如:最高工资3:
SELECT MIN(EmpSalary)
FROM Salary
WHERE EmpSalary IN(SELECT TOP 3 EmpSalary FROM Salary ORDER BY EmpSalary DESC)
N最低工资:
SELECT MAX(EmpSalary)
FROM Salary
WHERE EmpSalary IN(SELECT TOP N EmpSalary FROM Salary ORDER BY EmpSalary ASC)
例如:最低工资3:
SELECT MAX(EmpSalary)
FROM Salary
WHERE EmpSalary IN(SELECT TOP 3 EmpSalary FROM Salary ORDER BY EmpSalary ASC)
如果使用子查询,则太简单了!
SELECT MIN(EmpSalary) from (
SELECT EmpSalary from Employee ORDER BY EmpSalary DESC LIMIT 3
);
您可以在LIMIT约束后更改第n个值。
在此子查询中,按EmpSalary DESC限制3从员工订单中选择EmpSalary;将返回员工的前三名薪水。从结果中,我们将使用MIN命令选择最低薪水,以获取员工的第三名TOP薪水。
用您的最大号码替换N
SELECT *
FROM Employee Emp1
WHERE (N-1) = (
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary)
说明
如果您之前没有看到过类似的查询,则上面的查询可能会非常令人困惑-内部查询称为相关子查询,因为内部查询(子查询)使用外部查询的值(在这种情况下为Emp1表) )的WHERE子句中。
和来源
... WHERE (N-1) = (Subquery)...
起作用很重要。子查询是一个相关查询,因为其WHERE
子句Emp1
从主查询中使用。每次主查询扫描一行时都会评估子查询。例如,如果我们要从(800,1000,700,750)中找到第三大薪水(N = 3),则第一行的子查询将为SELECT COUNT(DISTINCT(Emp2.Salary)) FROM Employee Emp2 WHERE Emp2.Salary > 800
0。对于第四薪水值(750)... WHERE Emp2.Salary > 750
将为2,即N -1,因此将返回此行。
不使用子查询的薪水表中的第三或第n个最高薪水
select salary from salary
ORDER BY salary DESC
OFFSET N-1 ROWS
FETCH NEXT 1 ROWS ONLY
对于第三高薪,用2代替N-1
SELECT Salary,EmpName
FROM
(
SELECT Salary,EmpName,DENSE_RANK() OVER(ORDER BY Salary DESC) Rno from EMPLOYEE
) tbl
WHERE Rno=3
请参考以下查询以获得第n个最高薪水。通过这种方式,您在MYSQL中获得了第n高的薪水。如果您只想获得第n个最低工资,则需要在查询中将DESC替换为ASC。
SELECT EmpSalary
FROM salary_table
GROUP BY EmpSalary
ORDER BY EmpSalary DESC LIMIT n-1, 1;
方法1:
SELECT TOP 1 salary FROM (
SELECT TOP 3 salary
FROM employees
ORDER BY salary DESC) AS emp
ORDER BY salary ASC
方法2:
Select EmpName,salary from
(
select EmpName,salary ,Row_Number() over(order by salary desc) as rowid
from EmpTbl)
as a where rowid=3
在2008年,我们可以使用ROW_NUMBER()OVER(按EmpSalary DESC排序)来获得没有可使用关系的等级。
例如,我们可以通过这种方式获得第8位,或者将@N更改为其他值,或者根据需要将其用作函数中的参数。
DECLARE @N INT = 8;
WITH rankedSalaries AS
(
SELECT
EmpID
,EmpName
,EmpSalary,
,RN = ROW_NUMBER() OVER (ORDER BY EmpSalary DESC)
FROM salary
)
SELECT
EmpID
,EmpName
,EmpSalary
FROM rankedSalaries
WHERE RN = @N;
您可能知道,在SQL Server 2012中,这是使用LAG()更直观地执行的。
declare @maxNthSal as nvarchar(20)
SELECT TOP 3 @maxNthSal=GRN_NAME FROM GRN_HDR ORDER BY GRN_NAME DESC
print @maxNthSal
这是任何SQL面试中的热门问题之一。我将写下不同的查询以找出列的第n个最高值。
通过运行以下脚本,我创建了一个名为“员工”的表。
CREATE TABLE Employee([Eid] [float] NULL,[Ename] [nvarchar](255) NULL,[Basic_Sal] [float] NULL)
现在,我将通过在insert语句下面运行,将8行插入此表中。
insert into Employee values(1,'Neeraj',45000)
insert into Employee values(2,'Ankit',5000)
insert into Employee values(3,'Akshay',6000)
insert into Employee values(4,'Ramesh',7600)
insert into Employee values(5,'Vikas',4000)
insert into Employee values(7,'Neha',8500)
insert into Employee values(8,'Shivika',4500)
insert into Employee values(9,'Tarun',9500)
现在,我们将使用不同的查询从上表中找出第三高的Basic_sal。我在Management Studio中运行了以下查询,以下是结果。
select * from Employee order by Basic_Sal desc
我们可以从上图中看到,第三高的基本薪水是8500。我正在写3种不同的方法来做同样的事情。通过运行下面提到的所有三个查询,我们将得到相同的结果,即8500。
第一种方法:-使用行号功能
select Ename,Basic_sal
from(
select Ename,Basic_Sal,ROW_NUMBER() over (order by Basic_Sal desc) as rowid from Employee
)A
where rowid=2
SELECT MIN(COLUMN_NAME)
FROM (
SELECT DISTINCT TOP 3 COLUMN_NAME
FROM TABLE_NAME
ORDER BY
COLUMN_NAME DESC
) AS 'COLUMN_NAME'
优化方式:代替子查询,只使用限制。
select distinct salary from employee order by salary desc limit nth, 1;
从表中获得第三高的值
SELECT * FROM tableName ORDER BY columnName DESC LIMIT 2, 1
通过子查询:
SELECT salary from
(SELECT rownum ID, EmpSalary salary from
(SELECT DISTINCT EmpSalary from salary_table order by EmpSalary DESC)
where ID = nth)
试试这个查询
SELECT DISTINCT salary
FROM emp E WHERE
&no =(SELECT COUNT(DISTINCT salary)
FROM emp WHERE E.salary <= salary)
输入n =所需的值
set @n = $n
SELECT a.* FROM ( select a.* , @rn = @rn+1 from EMPLOYEE order by a.EmpSalary desc ) As a where rn = @n
试试这个代码:-
SELECT *
FROM one one1
WHERE ( n ) = ( SELECT COUNT( one2.salary )
FROM one one2
WHERE one2.salary >= one1.salary
)
select * from employee order by salary desc;
+------+------+------+-----------+
| id | name | age | salary |
+------+------+------+-----------+
| 5 | AJ | 20 | 100000.00 |
| 4 | Ajay | 25 | 80000.00 |
| 2 | ASM | 28 | 50000.00 |
| 3 | AM | 22 | 50000.00 |
| 1 | AJ | 24 | 30000.00 |
| 6 | Riu | 20 | 20000.00 |
+------+------+------+-----------+
select distinct salary from employee e1 where (n) = (select count( distinct(salary) ) from employee e2 where e1.salary<=e2.salary);
将n替换为第n个最高薪水。
试试这个...
SELECT MAX(salary) FROM employee WHERE salary NOT IN (SELECT * FROM employee ORDERBY salary DESC LIMIT n-1)
SELECT * FROM(按薪资DESC选择与客户订单不同的薪资)限制4,1;
限制4,1表示离开前4行,然后选择下一行。
限制和行号取决于您使用的平台。
试试这个,它将起作用。
子查询总是需要更多时间:
使用以下查询获取任何最高和最低数据:
最高数据: select *from business order by id desc limit 3,1;
最低数据: select *from business order by id asc limit 3,1;
可以使用N代替3获得第n个数据。