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

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

        从 oracle 为每个组选择最新的行

        时间:2023-09-17
          • <i id='tOS1a'><tr id='tOS1a'><dt id='tOS1a'><q id='tOS1a'><span id='tOS1a'><b id='tOS1a'><form id='tOS1a'><ins id='tOS1a'></ins><ul id='tOS1a'></ul><sub id='tOS1a'></sub></form><legend id='tOS1a'></legend><bdo id='tOS1a'><pre id='tOS1a'><center id='tOS1a'></center></pre></bdo></b><th id='tOS1a'></th></span></q></dt></tr></i><div id='tOS1a'><tfoot id='tOS1a'></tfoot><dl id='tOS1a'><fieldset id='tOS1a'></fieldset></dl></div>

                <bdo id='tOS1a'></bdo><ul id='tOS1a'></ul>

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

                  <tfoot id='tOS1a'></tfoot>
                1. <legend id='tOS1a'><style id='tOS1a'><dir id='tOS1a'><q id='tOS1a'></q></dir></style></legend>
                    <tbody id='tOS1a'></tbody>
                  本文介绍了从 oracle 为每个组选择最新的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在留言簿中有一张包含用户评论的表格.列是:id、user_id、title、comment、timestamp.

                  I have a table with user comments in a guestbook. Columns are: id, user_id, title, comment, timestamp.

                  我需要为每个用户选择最新的行.我曾尝试使用 group by 执行此操作,但尚未对其进行管理,因为我无法在按 user_id 分组的同一查询中选择任何其他内容:

                  I need to select the latest row for each user. I have tried to do this with group by but havent managed it because i cant select anything else in the same query where i group by user_id:

                  SELECT user_id, MAX(ts) FROM comments GROUP BY user_id
                  

                  例如在这个查询中,我不能添加到选择列 id、tilte 和 comment.这怎么办?

                  for example in this query i cant add to also select columns id, tilte and comment. How can this be done?

                  推荐答案

                  可以使用解析函数

                  SELECT *
                    FROM (SELECT c.*,
                                 rank() over (partition by user_id order by ts desc) rnk
                            FROM comments c)
                   WHERE rnk = 1
                  

                  根据您希望如何处理关系(如果可以有两行具有相同的 user_idts),您可能需要使用 row_numberdense_rank 函数而不是 rank.如果有平局,rank 将允许多行排在第一位.如果有平局,row_number 将任意返回一行.对于并列第一的行,dense_rank 的行为类似于 rank,但会认为下一行是第二行,而不是第三行,假设两行并列第一.

                  Depending on how you want to handle ties (if there can be two rows with the same user_id and ts), you may want to use the row_number or dense_rank function rather than rank. rank would allow multiple rows to be first if there was a tie. row_number would arbitrarily return one row if there was a tie. dense_rank would behave like rank for the rows that tied for first but would consider the next row to be second rather than third assuming two rows tie for first.

                  这篇关于从 oracle 为每个组选择最新的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:仅使用 SQL 将 Base 36 转换为 Base 10 下一篇:ORA-22905 - 使用 select 语句查询表类型时

                  相关文章

                2. <tfoot id='TUTbR'></tfoot>
                3. <legend id='TUTbR'><style id='TUTbR'><dir id='TUTbR'><q id='TUTbR'></q></dir></style></legend>

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

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

                      <bdo id='TUTbR'></bdo><ul id='TUTbR'></ul>