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

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

      2. 带聚合函数的 SQL GROUP BY CASE 语句

        时间:2024-04-16

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

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

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

                  <legend id='bKohe'><style id='bKohe'><dir id='bKohe'><q id='bKohe'></q></dir></style></legend>
                  本文介绍了带聚合函数的 SQL GROUP BY CASE 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一列看起来像这样:

                  I have a column that looks something like this:

                  CASE
                      WHEN col1 > col2 THEN SUM(col3*col4)
                      ELSE 0
                  END AS some_product
                  

                  我想把它放在我的 GROUP BY 子句中,但这似乎会导致问题,因为列中有一个聚合函数.在这种情况下,有没有办法 GROUP BY 列别名,例如 some_product,或者我是否需要将它放在子查询中并对其进行分组?

                  And I would like to put it in my GROUP BY clause, but this seems to cause problems because there is an aggregate function in column. Is there a way to GROUP BY a column alias such as some_product in this case, or do I need to put this in a subquery and group on that?

                  推荐答案

                  我猜你不是真的想要GROUP BY some_product.

                  My guess is that you don't really want to GROUP BY some_product.

                  回答:在这种情况下,有没有办法GROUP BY列别名,例如 some_product,或者我是否需要将其放在子查询中并组那个?"是:您不能GROUP BY列别名.

                  The answer to: "Is there a way to GROUP BY a column alias such as some_product in this case, or do I need to put this in a subquery and group on that?" is: You can not GROUP BY a column alias.

                  分配列别名的 SELECT 子句在 GROUP BY 子句之后才会处理.可以使用内联视图或公用表表达式 (CTE) 使结果可用于分组.

                  The SELECT clause, where column aliases are assigned, is not processed until after the GROUP BY clause. An inline view or common table expression (CTE) could be used to make the results available for grouping.

                  内联视图:

                  select ...
                  from (select ... , CASE WHEN col1 > col2 THEN SUM(col3*col4) ELSE 0 END AS some_product
                     from ...
                     group by col1, col2 ... ) T
                  group by some_product ...
                  

                  CTE:

                  with T as (select ... , CASE WHEN col1 > col2 THEN SUM(col3*col4) ELSE 0 END AS some_product
                     from ...
                     group by col1, col2 ... )
                  select ...
                  from T
                  group by some_product ... 
                  

                  这篇关于带聚合函数的 SQL GROUP BY CASE 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Django MySQLdb 版本与 _mysql 版本 Ubuntu 不匹配 下一篇:MySQL - 获取最低值

                  相关文章

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

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

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

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