Oracle中如何做到top 1?

发布于 2024-09-13 08:40:27 字数 193 浏览 11 评论 0原文

我该如何执行以下操作?

select top 1 Fname from MyTbl

Oracle 11g 中?

How do I do the following?

select top 1 Fname from MyTbl

In Oracle 11g?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

初吻给了烟 2024-09-20 08:40:28

使用:

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

如果使用 Oracle9i+,您可以查看 使用 ROW_NUMBER( )但它们的性能不如 ROWNUM

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.

木緿 2024-09-20 08:40:28

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

第一

select  max(Fname) from MyTbl;

第二

select  min(Fname) from MyTbl;

第三

select  Fname from MyTbl  where rownum = 1;

第四

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

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)
趁年轻赶紧闹 2024-09-20 08:40:28

我遇到了同样的问题,我可以使用此解决方案解决此问题:

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

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

祝你好运

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

可爱暴击 2024-09-20 08:40:27

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

select fname from MyTbl where rownum = 1

您还可以使用分析函数来排序并获取顶部的 x:

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

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:

select max(fname) over (rank() order by some_factor) from MyTbl
牵你的手,一向走下去 2024-09-20 08:40:27
SELECT *
  FROM (SELECT * FROM MyTbl ORDER BY Fname )
 WHERE ROWNUM = 1;
SELECT *
  FROM (SELECT * FROM MyTbl ORDER BY Fname )
 WHERE ROWNUM = 1;
要走就滚别墨迹 2024-09-20 08:40:27

使用 Oracle 12c(2013 年 6 月),您可以按如下方式使用它。

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

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

SELECT * FROM   MYTABLE
--ORDER BY COLUMNNAME -OPTIONAL          
OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY
_失温 2024-09-20 08:40:27

您可以在子查询中将 ROW_NUMBER()ORDER BY 子句结合使用,并使用此列替换 TOP N。这可以一步步解释。

请参阅下表,其中有两列 NAMEDT_CREATED

在此处输入图像描述

如果您只需要获取前两个日期,而不考虑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

在此处输入图像描述

在某些情况下,我们需要针对每个NAME选择TOP N结果。在这种情况下,我们可以在子查询中将 PARTITION BY 与 ORDER 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;

结果

在此输入图像描述

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

See the below table which have two columns NAME and DT_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

-- 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 N results respective to each NAME. In such case we can use PARTITION BY with an ORDER BY clause in sub-query. Refer the below query.

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

友谊不毕业 2024-09-20 08:40:27
select * from (
    select FName from MyTbl
)
where rownum <= 1;
select * from (
    select FName from MyTbl
)
where rownum <= 1;
合久必婚 2024-09-20 08:40:27

您可以执行类似的操作,

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

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

You can do something like

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

You could also use the analytic functions RANK and/or DENSE_RANK, but ROWNUM is probably the easiest.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文