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

时间:2020-03-05 18:58:09  来源:igfitidea点击:

我想找到一列的2,3rd..nth个最大值

解决方案

回答

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

编辑::

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

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

类似于上面的内容对于Oracle应该适用...我们可能必须首先正确使用语法!

回答

我们没有指定可以在MySQL上执行的数据库

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

将跳过前7个,然后再向后10个最高。

回答

在SQL Server中,只需执行以下操作:

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

然后,如果不需要,则丢弃第一个值。

回答

同样,我们可能需要修复数据库,但是如果我们希望数据集中的前第二个值可能具有重复的值,则还需要进行分组:

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

将跳过前两个,然后将为我们提供下一个最高的五个。

回答

纯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 | 
+------+

回答

这是用于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。

回答

对于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

回答

针对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

回答

自己寻找答案时,只需挖出这个问题即可,这似乎适用于SQL Server 2005(源自Blorgbeard的解决方案):

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

实际上,这是一个来自someTable q的SELECT MIN(q.someCol),该表的前n个是通过SELECT DISTINCT ...查询来检索的。

回答

考虑下面的"雇员"表,其中"工资"列为一列。

+------+
| 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 |
+------+

回答

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;

回答

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

要找到第N个最高工资。

回答

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

回答

MySQL:

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