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

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

  2. <small id='VOfG9'></small><noframes id='VOfG9'>

      SQL Server:只有 GROUP BY 中的最后一个条目

      时间:2024-04-16

      <small id='2zOTb'></small><noframes id='2zOTb'>

          <tbody id='2zOTb'></tbody>

          <bdo id='2zOTb'></bdo><ul id='2zOTb'></ul>

                <tfoot id='2zOTb'></tfoot>

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

              1. 本文介绍了SQL Server:只有 GROUP BY 中的最后一个条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我在 MSSQL2005 中有下表

                I have the following table in MSSQL2005

                id | business_key | result
                1 | 1 | 0
                2 | 1 | 1
                3 | 2 | 1
                4 | 3 | 1
                5 | 4 | 1
                6 | 4 | 0
                

                现在我想根据 business_key 进行分组,返回具有最高 ID 的完整条目.所以我的预期结果是:

                And now i want to group based on the business_key returning the complete entry with the highest id. So my expected result is:

                business_key | result
                1 | 1
                2 | 1
                3 | 1
                4 | 0
                

                我敢打赌有一种方法可以实现这一目标,但目前我看不到.

                I bet that there is a way to achieve that, i just can't see it at the moment.

                推荐答案

                另一种解决方案,它可能会给您带来更好的性能(测试两种方式并检查执行计划):

                An alternative solution, which may give you better performance (test both ways and check the execution plans):

                SELECT
                     T1.id,
                     T1.business_key,
                     T1.result
                FROM
                     dbo.My_Table T1
                LEFT OUTER JOIN dbo.My_Table T2 ON
                     T2.business_key = T1.business_key AND
                     T2.id > T1.id
                WHERE
                     T2.id IS NULL
                

                这个查询假设 ID 是一个唯一值(至少对于任何给定的 business_key)并且它被设置为 NOT NULL.

                This query assumes that the ID is a unique value (at least for any given business_key) and that it is set to NOT NULL.

                这篇关于SQL Server:只有 GROUP BY 中的最后一个条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:将记录按小时或按天分组,并在 mysql 中用零或空值填充空白 下一篇:如何在 MySQL 中创建一个连续的组?

                相关文章

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

                    <tfoot id='UvMo0'></tfoot>

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