<legend id='vnzHR'><style id='vnzHR'><dir id='vnzHR'><q id='vnzHR'></q></dir></style></legend>

  • <tfoot id='vnzHR'></tfoot>

      <small id='vnzHR'></small><noframes id='vnzHR'>

        <bdo id='vnzHR'></bdo><ul id='vnzHR'></ul>
      1. <i id='vnzHR'><tr id='vnzHR'><dt id='vnzHR'><q id='vnzHR'><span id='vnzHR'><b id='vnzHR'><form id='vnzHR'><ins id='vnzHR'></ins><ul id='vnzHR'></ul><sub id='vnzHR'></sub></form><legend id='vnzHR'></legend><bdo id='vnzHR'><pre id='vnzHR'><center id='vnzHR'></center></pre></bdo></b><th id='vnzHR'></th></span></q></dt></tr></i><div id='vnzHR'><tfoot id='vnzHR'></tfoot><dl id='vnzHR'><fieldset id='vnzHR'></fieldset></dl></div>

        如何正确使用 Oracle ORDER BY 和 ROWNUM?

        时间:2023-09-19
          <tbody id='kSq6X'></tbody>
              <bdo id='kSq6X'></bdo><ul id='kSq6X'></ul>

            • <small id='kSq6X'></small><noframes id='kSq6X'>

              • <tfoot id='kSq6X'></tfoot>

                <i id='kSq6X'><tr id='kSq6X'><dt id='kSq6X'><q id='kSq6X'><span id='kSq6X'><b id='kSq6X'><form id='kSq6X'><ins id='kSq6X'></ins><ul id='kSq6X'></ul><sub id='kSq6X'></sub></form><legend id='kSq6X'></legend><bdo id='kSq6X'><pre id='kSq6X'><center id='kSq6X'></center></pre></bdo></b><th id='kSq6X'></th></span></q></dt></tr></i><div id='kSq6X'><tfoot id='kSq6X'></tfoot><dl id='kSq6X'><fieldset id='kSq6X'></fieldset></dl></div>
                  <legend id='kSq6X'><style id='kSq6X'><dir id='kSq6X'><q id='kSq6X'></q></dir></style></legend>
                1. 本文介绍了如何正确使用 Oracle ORDER BY 和 ROWNUM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我很难将存储过程从 SQL Server 转换为 Oracle,以使我们的产品与之兼容.

                  I am having a hard time converting stored procedures from SQL Server to Oracle to have our product compatible with it.

                  我有一些查询会根据时间戳返回某些表的最新记录:

                  I have queries which returns the most recent record of some tables, based on a timestamp :

                  SQL Server:

                  SELECT TOP 1 *
                  FROM RACEWAY_INPUT_LABO
                  ORDER BY t_stamp DESC
                  

                  => 这将返回我最近的记录

                  => That will returns me the most recent record

                  但是甲骨文:

                  SELECT *
                  FROM raceway_input_labo 
                  WHERE  rownum <= 1
                  ORDER BY t_stamp DESC
                  

                  => 这将返回最旧的记录(可能取决于索引),无论 ORDER BY 语句如何!

                  => That will returns me the oldest record (probably depending on the index), regardless the ORDER BY statement!

                  我以这种方式封装了 Oracle 查询以符合我的要求:

                  I encapsulated the Oracle query this way to match my requirements:

                  SELECT * 
                  FROM 
                      (SELECT *
                       FROM raceway_input_labo 
                       ORDER BY t_stamp DESC)
                  WHERE  rownum <= 1
                  

                  它的工作原理.但这对我来说听起来像是一个可怕的黑客,特别是如果我在涉及的表中有很多记录.

                  and it works. But it sounds like a horrible hack to me, especially if I have a lot of records in the involved tables.

                  实现这一目标的最佳方法是什么?

                  What is the best way to achieve this ?

                  推荐答案

                  where 语句在 order by 之前执行.因此,您想要的查询是取第一行,然后按 t_stamp desc 排序".这不是你想要的.

                  The where statement gets executed before the order by. So, your desired query is saying "take the first row and then order it by t_stamp desc". And that is not what you intend.

                  子查询方法是在 Oracle 中执行此操作的正确方法.

                  The subquery method is the proper method for doing this in Oracle.

                  如果你想要一个在两台服务器上都能运行的版本,你可以使用:

                  If you want a version that works in both servers, you can use:

                  select ril.*
                  from (select ril.*, row_number() over (order by t_stamp desc) as seqnum
                        from raceway_input_labo ril
                       ) ril
                  where seqnum = 1
                  

                  外部 * 将在最后一列返回1".您需要单独列出列以避免这种情况.

                  The outer * will return "1" in the last column. You would need to list the columns individually to avoid this.

                  这篇关于如何正确使用 Oracle ORDER BY 和 ROWNUM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:ORA-28040: 没有匹配的身份验证协议异常 下一篇:如何生成 Oracle 模式的整个 DDL(可编写脚本)?

                  相关文章

                  1. <i id='rLETX'><tr id='rLETX'><dt id='rLETX'><q id='rLETX'><span id='rLETX'><b id='rLETX'><form id='rLETX'><ins id='rLETX'></ins><ul id='rLETX'></ul><sub id='rLETX'></sub></form><legend id='rLETX'></legend><bdo id='rLETX'><pre id='rLETX'><center id='rLETX'></center></pre></bdo></b><th id='rLETX'></th></span></q></dt></tr></i><div id='rLETX'><tfoot id='rLETX'></tfoot><dl id='rLETX'><fieldset id='rLETX'></fieldset></dl></div>
                      • <bdo id='rLETX'></bdo><ul id='rLETX'></ul>
                    1. <legend id='rLETX'><style id='rLETX'><dir id='rLETX'><q id='rLETX'></q></dir></style></legend>

                      <small id='rLETX'></small><noframes id='rLETX'>

                      <tfoot id='rLETX'></tfoot>