查询以查找列的第n个最大值

时间:2008-09-17 07:10:24

标签: sql database

我想找到列的第2,第3 ......最大值

28 个答案:

答案 0 :(得分:29)

考虑以下Employee表,其中包含一列薪水。

+------+
| Sal  |
+------+
| 3500 | 
| 2500 | 
| 2500 | 
| 5500 |
| 7500 |
+------+

以下查询将返回第N个最大元素。

select SAL from EMPLOYEE E1 where 
 (N - 1) = (select count(distinct(SAL)) 
            from EMPLOYEE E2 
            where E2.SAL > E1.SAL )

例如。当需要第二个最大值时,

  select SAL from EMPLOYEE E1 where 
     (2 - 1) = (select count(distinct(SAL)) 
                from EMPLOYEE E2 
                where E2.SAL > E1.SAL )
+------+
| Sal  |
+------+
| 5500 |
+------+

答案 1 :(得分:11)

您可以将列排序为降序格式,然后从第n行获取值。

EDIT ::

根据评论请求更新。 警告 完全未经测试!

SELECT DOB FROM (SELECT DOB FROM USERS ORDER BY DOB DESC) WHERE ROWID = 6

上述内容应该适用于Oracle ...您可能必须首先获得正确的语法!

答案 2 :(得分:8)

您没有在MySQL上指定哪个数据库

SELECT column FROM table ORDER BY column DESC LIMIT 7,10;

将跳过前7个,然后让你获得下一个最高的10个。

答案 3 :(得分:6)

同样,您可能需要修复数据库,但如果您希望数据集中的前2位值可能具有重复值,那么您还需要执行一个组:

SELECT column 
FROM table 
WHERE column IS NOT NULL 
GROUP BY column 
ORDER BY column DESC 
LIMIT 5 OFFSET 2;

会跳过前两个,然后会让你接下来的五个最高。

答案 4 :(得分:5)

纯SQL(注意:我建议使用特定于您的DBMS的SQL功能,因为它可能更有效)。这将获得第n + 1个最大值(最小值,翻转<)。如果您有重复项,请将其设为COUNT(DISTINCT VALUE)..

select id from table order by id desc limit 4 ;
+------+
| id   |
+------+
| 2211 | 
| 2210 | 
| 2209 | 
| 2208 | 
+------+


SELECT yourvalue
  FROM yourtable t1
 WHERE EXISTS( SELECT COUNT(*)
                 FROM yourtable t2
                WHERE t1.id       <> t2.id
                  AND t1.yourvalue < t2.yourvalue
               HAVING COUNT(*) = 3 )


+------+
| id   |
+------+
| 2208 | 
+------+

答案 5 :(得分:3)

(表名=学生,列名=标记)

select * from(select row_number() over (order by mark desc) as t,mark from student group by mark) as td where t=4

答案 6 :(得分:2)

您可以使用以下查询找到列的第n个最大值:

SELECT * FROM TableName a WHERE
    n = (SELECT count(DISTINCT(b.ColumnName)) 
    FROM TableName b WHERE a.ColumnName <=b.ColumnName);

答案 7 :(得分:1)

这是Oracle的一种方法。此示例获得第9个最高值。只需用包含您要查找的位置的绑定变量替换9即可。

   select created from (
     select created from (
       select created from user_objects
         order by created desc
       )
       where rownum <= 9
       order by created asc
     )
     where rownum = 1

如果你想要第n个唯一值,你可以在最里面的查询块上添加DISTINCT。

答案 8 :(得分:1)

在我自己寻找答案时只是挖出这个问题,这似乎适用于SQL Server 2005(源自Blorgbeard's solution):

SELECT MIN(q.col1) FROM (
    SELECT
        DISTINCT TOP n col1
        FROM myTable
        ORDER BY col1 DESC
) q;

实际上,这是SELECT MIN(q.someCol) FROM someTable qSELECT DISTINCT...查询检索到表格的前n位。

答案 9 :(得分:1)

select column_name from table_name 
order by column_name desc limit n-1,1;

其中n = 1,2,3,.... nth max value。

答案 10 :(得分:1)

Select max(sal) 
from table t1 
where N (select max(sal) 
        from table t2 
        where t2.sal > t1.sal)

找到Nth max sal。

答案 11 :(得分:1)

SELECT * FROM tablename 
WHERE columnname<(select max(columnname) from tablename) 
order by columnname desc limit 1

答案 12 :(得分:1)

这是从列中获得第n个最高的查询,n = 0表示第二高,n = 1表示第三高等等...

 SELECT * FROM TableName
 WHERE ColomnName<(select max(ColomnName) from TableName)-n order by ColomnName desc limit 1;

答案 13 :(得分:0)

mysql查询假设我要找出雇员人数表中的第n个最高薪水表

select salary 
form employee
order by salary desc
limit n-1,1 ;

答案 14 :(得分:0)

在PostgreSQL中,从Employee表中找到第N个最大薪水。

SELECT * FROM Employee WHERE salary in 
(SELECT salary FROM Employee ORDER BY salary DESC LIMIT N) 
ORDER BY salary ASC LIMIT 1;

答案 15 :(得分:0)

在SQL Server中查找特定列的第N个最大值的解决方案:

员工表:

Employee Table

销售表:

Sales Table

员工表数据:

==========
Id  name
=========
6   ARSHAD M
7   Manu
8   Shaji

销售表数据:

=================
id  emp_id   amount
=================
1   6        500
2   7        100
3   8        100
4   6        150
5   7        130
6   7        130
7   7        330

查询以查找销售最高/第N位最高销售人员的员工的详细信息

select * from (select E.Id,E.name,SUM(S.amount) AS 'total_amount' from employee E INNER JOIN Sale S on E.Id=S.emp_id group by S.emp_id,E.Id,E.name ) AS T1 WHERE(0)=( select COUNT(DISTINCT(total_amount)) from(select E.Id,E.name,SUM(S.amount) AS 'total_amount' from employee E INNER JOIN Sale S on E.Id=S.emp_id group by S.emp_id,E.Id,E.name )AS T2 WHERE(T1.total_amount<T2.total_amount) );

WHERE(0)中将0替换为n-1

结果:

========================
id  name    total_amount
========================
7   Manu    690

答案 16 :(得分:0)

简单SQL查询,以获取表Salary中具有第N个MAX Employee的员工详细信息。

sql> select * from Employee order by salary desc LIMIT 1 OFFSET <N - 1>;

将表结构视为:

  

员工(         id [int主键auto_increment],         name [varchar(30)],         salary [int]);

示例:

如果您需要上表中的第3 MAX个工资,那么查询将是:

sql> select * from Employee order by salary desc LIMIT 1 OFFSET 2;

<强>类似地:

如果您需要上表中的第8个MAX工资,那么查询将是:

sql> select * from Employee order by salary desc LIMIT 1 OFFSET 7;
  

注意:   当您必须获取第N个 MAX值时,您应该将OFFSET作为(N - 1)

就像这样,你可以按工资递增的顺序进行同样的操作。

答案 17 :(得分:0)

你可以像这样简化

SELECT MIN(Sal) FROM TableName
WHERE Sal IN
(SELECT TOP 4 Sal FROM TableName ORDER BY Sal DESC)

如果Sal包含重复值,请使用此

SELECT MIN(Sal) FROM TableName
WHERE Sal IN
(SELECT distinct TOP 4 Sal FROM TableName ORDER BY Sal DESC)

4将是第n个值,它可以是任何最高值,例如5或6等。

答案 18 :(得分:0)

Select min(fee) 
from fl_FLFee 
where fee in (Select top 4 Fee from fl_FLFee order by 1 desc)

用N改变第四。

答案 19 :(得分:0)

我认为下面的查询在oracle sql上工作得非常完美...我自己测试了它..

与此查询相关的信息:此查询使用两个名为employeedepartment的表,其中员工名为name(员工姓名),dept_id(常见)到员工和部门),salary

部门表中的列:dept_id(员工表也常见),dept_name

SELECT
  tab.dept_name,MIN(tab.salary) AS Second_Max_Sal FROM (
    SELECT e.name, e.salary, d.dept_name, dense_rank() over (partition BY  d.dept_name          ORDER BY e.salary)  AS   rank FROM department d JOIN employee e USING (dept_id) )  tab
 WHERE
   rank  BETWEEN 1 AND 2
 GROUP BY
   tab.dept_name

感谢

答案 20 :(得分:0)

(TableName = Student,ColumnName = Mark):

select *
from student 
where mark=(select mark 
            from(select row_number() over (order by mark desc) as t,
                 mark 
                 from student group by mark) as td 
            where t=2)

答案 21 :(得分:0)

答案: 第二名:

select * from (select * from deletetable   where rownum <=2 order by rownum desc) where rownum <=1

答案 22 :(得分:0)

MySQL的:

select distinct(salary) from employee order by salary desc limit (n-1), 1;

答案 23 :(得分:0)

select sal,ename from emp e where
 2=(select count(distinct sal) from emp  where e.sal<=emp.sal) or
 3=(select count(distinct sal) from emp  where e.sal<=emp.sal) or
 4=(select count(distinct sal) from emp  where e.sal<=emp.sal) order by sal desc;

答案 24 :(得分:0)

另一个使用分析函数的Oracle:

select distinct col1 --distinct is required to remove matching value of column
from 
( select col1, dense_rank() over (order by col1 desc) rnk
  from tbl
)
where rnk = :b1

答案 25 :(得分:0)

for SQL 2005:

SELECT col1 from 
     (select col1, dense_rank(col1) over (order by col1 desc) ranking 
     from t1) subq where ranking between 2 and @n

答案 26 :(得分:0)

在SQL Server中,只需执行:

select distinct top n+1 column from table order by column desc

然后扔掉第一个值,如果你不需要它。

答案 27 :(得分:-1)

表员工

salary 
1256
1256
2563
8546
5645

您可以通过此查询找到第二个最大值

select salary 
from employee 
where salary=(select max(salary) 
                from employee 
                where salary <(select max(salary) from employee));

您可以通过此查询找到第三个最大值

select salary 
from employee 
where salary=(select max(salary) 
                from employee 
                where salary <(select max(salary) 
                                from employee 
                                where salary <(select max(salary)from employee)));