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)
您可以在子查询中将 ROW_NUMBER() 与 ORDER BY 子句结合使用,并使用此列替换 TOP N。这可以一步步解释。
请参阅下表,其中有两列 NAME 和 DT_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.
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
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;
发布评论
评论(9)
使用:
如果使用 Oracle9i+,您可以查看 使用 ROW_NUMBER( )但它们的性能不如 ROWNUM。
Use:
If using Oracle9i+, you could look at using analytic functions like ROW_NUMBER() but they won't perform as well as ROWNUM.
从表中选择第一行和从表中选择一行是两个不同的任务,需要不同的查询。有很多可能的方法可以做到这一点。其中四个是:
第一
第二
第三
第四
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
Second
Third
Fourth
我遇到了同样的问题,我可以使用此解决方案解决此问题:
您可以先对结果进行排序,以便将第一个值放在上面。
祝你好运
I had the same issue, and I can fix this with this solution:
You can order your result before to have the first value on top.
Good luck
如果您只想要第一个选定的行,您可以:
您还可以使用分析函数来排序并获取顶部的 x:
If you want just a first selected row, you can:
You can also use analytic functions to order and take the top x:
使用 Oracle 12c(2013 年 6 月),您可以按如下方式使用它。
With Oracle 12c (June 2013), you are able to use it like the following.
您可以在子查询中将
ROW_NUMBER()
与ORDER BY
子句结合使用,并使用此列替换TOP N
。这可以一步步解释。请参阅下表,其中有两列
NAME
和DT_CREATED
。如果您只需要获取前两个日期,而不考虑
NAME
,则可以使用以下查询。逻辑已写入查询RESULT
在某些情况下,我们需要针对每个
NAME
选择TOP N
结果。在这种情况下,我们可以在子查询中将 PARTITION BY 与 ORDER BY 子句一起使用。请参阅以下查询。结果
You could use
ROW_NUMBER()
with aORDER BY
clause in sub-query and use this column in replacement ofTOP N
. This can be explained step-by-step.See the below table which have two columns
NAME
andDT_CREATED
.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 queryRESULT
In some situations, we need to select
TOP N
results respective to eachNAME
. In such case we can usePARTITION BY
with anORDER BY
clause in sub-query. Refer the below query.RESULT
您可以执行类似的操作,
您也可以使用分析函数 RANK 和/或 DENSE_RANK,但 ROWNUM 可能是最简单的。
You can do something like
You could also use the analytic functions RANK and/or DENSE_RANK, but ROWNUM is probably the easiest.