<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 中的最后一个条目

      SQL Server: Only last entry in GROUP BY(SQL Server:只有 GROUP BY 中的最后一个条目)

      <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 中的最后一个条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                SQL query to group by day(按天分组的 SQL 查询)
                What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                Include missing months in Group By query(在 Group By 查询中包含缺失的月份)
                Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)
                <legend id='UvMo0'><style id='UvMo0'><dir id='UvMo0'><q id='UvMo0'></q></dir></style></legend>

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

                      <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>
                        • <small id='UvMo0'></small><noframes id='UvMo0'>