SQL 我如何在 Oracle 中排名前 1?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3451534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 07:09:48  来源:igfitidea点击:

How do I do top 1 in Oracle?

sqloracleoracle11ggreatest-n-per-group

提问by Gold

How do I do the following?

我如何执行以下操作?

select top 1 Fname from MyTbl

In Oracle 11g?

Oracle 11g 中

回答by mcpeterson

If you want just a first selected row, you can:

如果您只想要第一个选定的行,您可以:

select fname from MyTbl where rownum = 1

You can also use analytic functions to order and take the top x:

您还可以使用解析函数对顶部 x 进行排序和取值:

select max(fname) over (rank() order by some_factor) from MyTbl

回答by Damian Leszczyński - Vash

SELECT *
  FROM (SELECT * FROM MyTbl ORDER BY Fname )
 WHERE ROWNUM = 1;

回答by MSK

With Oracle 12c(June 2013), you are able to use it like the following.

使用Oracle 12c(2013 年 6 月),您可以像下面这样使用它。

SELECT * FROM   MYTABLE
--ORDER BY COLUMNNAME -OPTIONAL          
OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY

回答by Sarath Avanavu

You could use ROW_NUMBER()with a ORDER BYclause in sub-query and use this column in replacement of TOP N. This can be explained step-by-step.

您可以在子查询中使用ROW_NUMBER()withORDER BY子句并使用此列替换TOP N. 这可以逐步解释。

See the below table which have two columns NAMEand DT_CREATED.

请参阅下表,其中包含两列NAMEDT_CREATED

enter image description here

在此处输入图片说明

If you need to take only the first two dates irrespective of NAME, you could use the below query. The logic has been written inside query

如果您只需要取前两个日期而不考虑NAME,您可以使用以下查询。逻辑已经写在查询里面

-- The number of records can be specified in WHERE clause
SELECT RNO,NAME,DT_CREATED
FROM
(
    -- Generates numbers in a column in sequence in the order of date
    SELECT ROW_NUMBER() OVER (ORDER BY DT_CREATED) AS RNO,
    NAME,DT_CREATED
    FROM DEMOTOP
)TAB
WHERE RNO<3;

RESULT

结果

enter image description here

在此处输入图片说明

In some situations, we need to select TOP Nresults respective to each NAME. In such case we can use PARTITION BYwith an ORDER BYclause in sub-query. Refer the below query.

在某些情况下,我们需要TOP N为每个选择结果NAME。在这种情况下,我们可以使用PARTITION BYORDER BY子查询子句。请参阅以下查询。

-- The number of records can be specified in WHERE clause
SELECT RNO,NAME,DT_CREATED
FROM
(
  --Generates numbers in a column in sequence in the order of date for each NAME
    SELECT ROW_NUMBER() OVER (PARTITION BY NAME ORDER BY DT_CREATED) AS RNO,
    NAME,DT_CREATED
    FROM DEMOTOP
)TAB
WHERE RNO<3;

RESULT

结果

enter image description here

在此处输入图片说明

回答by Suman

You can do something like

你可以做类似的事情

    SELECT *
      FROM (SELECT Fname FROM MyTbl ORDER BY Fname )
 WHERE rownum = 1;

You could also use the analytic functions RANKand/or DENSE_RANK, but ROWNUMis probably the easiest.

您还可以使用分析函数RANK和/或DENSE_RANK,但ROWNUM可能是最简单的。

回答by a'r

select * from (
    select FName from MyTbl
)
where rownum <= 1;

回答by OMG Ponies

Use:

用:

SELECT x.*
  FROM (SELECT fname 
          FROM MyTbl) x
 WHERE ROWNUM = 1

If using Oracle9i+, you could look at using analytic functions like ROW_NUMBER() but they won't perform as well as ROWNUM.

如果使用 Oracle9i+,您可以考虑使用诸如 ROW_NUMBER() 之类的分析函数,但它们的性能不如 ROWNUM

回答by Vikas Hardia

To select the first row from a table and to select one row from a table are two different tasks and need a different query. There are many possible ways to do so. Four of them are:

从表中选择第一行和从表中选择一行是两个不同的任务,需要不同的查询。有很多可能的方法可以做到这一点。其中四个是:

First

第一的

select  max(Fname) from MyTbl;

Second

第二

select  min(Fname) from MyTbl;

Third

第三

select  Fname from MyTbl  where rownum = 1;

Fourth

第四

select  max(Fname) from MyTbl where rowid=(select  max(rowid) from MyTbl)

回答by user2607028

I had the same issue, and I can fix this with this solution:

我有同样的问题,我可以用这个解决方案解决这个问题:

select a.*, rownum 
from (select Fname from MyTbl order by Fname DESC) a
where
rownum = 1

You can order your result before to have the first value on top.

您可以先对结果进行排序,以便将第一个值放在最前面。

Good luck

祝你好运