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

    1. <tfoot id='YcYXd'></tfoot>

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

        根据由另一列分组的不同列的最大值获取值

        时间:2023-09-20
          • <bdo id='h5qzb'></bdo><ul id='h5qzb'></ul>

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

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

              • <tfoot id='h5qzb'></tfoot>
                  <tbody id='h5qzb'></tbody>

                  <i id='h5qzb'><tr id='h5qzb'><dt id='h5qzb'><q id='h5qzb'><span id='h5qzb'><b id='h5qzb'><form id='h5qzb'><ins id='h5qzb'></ins><ul id='h5qzb'></ul><sub id='h5qzb'></sub></form><legend id='h5qzb'></legend><bdo id='h5qzb'><pre id='h5qzb'><center id='h5qzb'></center></pre></bdo></b><th id='h5qzb'></th></span></q></dt></tr></i><div id='h5qzb'><tfoot id='h5qzb'></tfoot><dl id='h5qzb'><fieldset id='h5qzb'></fieldset></dl></div>
                  本文介绍了根据由另一列分组的不同列的最大值获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想根据由另一列分组的不同列的最大值来获取列的值.

                  I want to get the values of a column based on max of a different column grouped by another column.

                  我有这张桌子:

                  KEY NUM VAL
                  A   1   AB
                  B   1   CD
                  B   2   EF
                  C   2   GH
                  C   3   HI
                  D   1   JK
                  D   3   LM
                  

                  想要这个结果:

                  KEY VAL
                  A   AB
                  B   EF
                  C   HI
                  D   LM
                  

                  我实际上可以使用这个查询来获取它.

                  I could actually use this query to get it.

                  select KEY, VAL
                  from TABLE_NAME TN
                  where NUM = (
                      select max(NUM)
                      from TABLE_NAME TMP
                      where TMP.KEY = TN.KEY
                      )
                  

                  但是在 ORACLE SQL(10g 或以上)中有更优雅的方式来获得结果吗?

                  However is there a more elegant way in ORACLE SQL (10g or above) to get the result?

                  背后的原因是实际上有多个KEY,只是看起来有点难看.

                  Reason behind it is that there are actually multiple KEYs and it just looks a bit ugly.

                  推荐答案

                  您可以使用 row_number() 来解决这个问题:

                  You can approach this using row_number():

                  select key, val
                  from (select t.*, row_number() over (partition by key order by num desc) as seqnum
                        from table_name t
                       ) t
                  where seqnum = 1;
                  

                  你是否认为这更优雅"可能是一个品味问题.

                  Whether you consider this more "elegant" is probably a matter of taste.

                  我应该指出,这与您的查询略有不同.这保证为每个 key 返回一行;你的可以返回多行.如果您想要这种行为,只需使用 rank()dense_rank() 而不是 row_number().

                  I should point out that this is subtly different from your query. This is guaranteed to return one row for each key; yours could return multiple rows. If you want that behavior, just use rank() or dense_rank() instead of row_number().

                  这篇关于根据由另一列分组的不同列的最大值获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:SQL 中的 GROUP BY/聚合函数混淆 下一篇:在 SQL Developer 中打印变量的值

                  相关文章

                  1. <tfoot id='3Ntk5'></tfoot>
                  2. <small id='3Ntk5'></small><noframes id='3Ntk5'>

                    • <bdo id='3Ntk5'></bdo><ul id='3Ntk5'></ul>

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

                    1. <legend id='3Ntk5'><style id='3Ntk5'><dir id='3Ntk5'><q id='3Ntk5'></q></dir></style></legend>