• <small id='17S6I'></small><noframes id='17S6I'>

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

    1. <tfoot id='17S6I'></tfoot>
      <legend id='17S6I'><style id='17S6I'><dir id='17S6I'><q id='17S6I'></q></dir></style></legend>

          <bdo id='17S6I'></bdo><ul id='17S6I'></ul>

        如何按计算字段分组

        时间:2024-04-16
      1. <i id='nhm93'><tr id='nhm93'><dt id='nhm93'><q id='nhm93'><span id='nhm93'><b id='nhm93'><form id='nhm93'><ins id='nhm93'></ins><ul id='nhm93'></ul><sub id='nhm93'></sub></form><legend id='nhm93'></legend><bdo id='nhm93'><pre id='nhm93'><center id='nhm93'></center></pre></bdo></b><th id='nhm93'></th></span></q></dt></tr></i><div id='nhm93'><tfoot id='nhm93'></tfoot><dl id='nhm93'><fieldset id='nhm93'></fieldset></dl></div>

          • <bdo id='nhm93'></bdo><ul id='nhm93'></ul>
              <tbody id='nhm93'></tbody>

            • <legend id='nhm93'><style id='nhm93'><dir id='nhm93'><q id='nhm93'></q></dir></style></legend>
                <tfoot id='nhm93'></tfoot>

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

                  本文介绍了如何按计算字段分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要在 SQL Server 2005/2008 中按计算字段分组.

                  I need to group by a Calculated field ins SQL Server 2005/2008.

                  我有以下sql:

                  select dateadd(day, -7, Convert(DateTime, mwspp.DateDue) + (7 - datepart(weekday, mwspp.DateDue))),
                  sum(mwspp.QtyRequired)
                  from manufacturingweekshortagepartpurchasing mwspp
                  where mwspp.buildScheduleSimID = 10109 and mwspp.partID = 8366
                  group by mwspp.DateDue
                  order by mwspp.DateDue
                  

                  不是 group by mwspp.DateDue 我需要按计算结果分组.可能吗?

                  Instead of group by mwspp.DateDue I need to group by the result of the calculation. Is it possible ?

                  提前致谢

                  推荐答案

                  当然,只需在 GROUP BY 子句中添加相同的计算:

                  Sure, just add the same calculation to the GROUP BY clause:

                  select dateadd(day, -7, Convert(DateTime, mwspp.DateDue) + (7 - datepart(weekday, mwspp.DateDue))),
                  sum(mwspp.QtyRequired)
                  from manufacturingweekshortagepartpurchasing mwspp
                  where mwspp.buildScheduleSimID = 10109 and mwspp.partID = 8366
                  group by dateadd(day, -7, Convert(DateTime, mwspp.DateDue) + (7 - datepart(weekday, mwspp.DateDue)))
                  order by dateadd(day, -7, Convert(DateTime, mwspp.DateDue) + (7 - datepart(weekday, mwspp.DateDue)))
                  

                  评论后

                  就像所有关于优化器的问题一样,答案实际上是视情况而定",但很可能只会执行一次 - 您会在执行计划中看到它作为 Compute Scalar 运算符.

                  Like all questions regarding the optimiser, the answer is really "it depends", but most likely it will only be performed once - you'll see this in the execution plan as a Compute Scalar operator.

                  基于这个计算标量操作,优化器将决定如何执行实际的聚合.

                  Based on this Compute Scalar operation, the optimiser will then decide how to perform the actual aggregation.

                  这里的其他答案(CTE/子查询等)都同样有效,但并没有真正改变逻辑 - 最终它们将执行类似的操作.SQL Server 可能会以不同的方式对待它们,但这不太可能.但是,它们将有助于提高可读性.

                  The other answers here (CTE/subquery, etc) are all equally valid, but don't really change the logic - ultimately they will be performing similar operations. SQL Server might treat them differently but it's unlikely. However they will help with readability.

                  如果您担心效率,可以查看几个选项,例如将计算设置为持久计算列并使用它在索引中,或将临时结果添加到临时表中.

                  If you're worried about efficiency you can look at a couple of options, e.g. setting up the calculation as a persisted computed column and using this in an index, or adding interim results into a temporary table.

                  真正确定的唯一方法是在对典型数据集运行查询时检查执行计划/IO 统计信息,并查看您是否对性能感到满意;如果没有,也许可以调查上述选项之一.

                  The only way to really know for sure is to inspect the Execution Plan/IO statistics when running the query on a typical data set and seeing if you're satisfied with the performance; if not, perhaps investigating one of the above options.

                  这篇关于如何按计算字段分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何获取 GROUP_BY 子句中的值列表? 下一篇:Mysql查询以获取每月计数

                  相关文章

                    1. <small id='BNouZ'></small><noframes id='BNouZ'>

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

                      <tfoot id='BNouZ'></tfoot>

                    3. <legend id='BNouZ'><style id='BNouZ'><dir id='BNouZ'><q id='BNouZ'></q></dir></style></legend>
                        <bdo id='BNouZ'></bdo><ul id='BNouZ'></ul>