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

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

        • <bdo id='UqPsM'></bdo><ul id='UqPsM'></ul>

        列“在选择列表中无效,因为它不包含在聚合函数或 GROUP BY 子句中"

        时间:2023-07-17
          <tbody id='F4gUv'></tbody>
      1. <legend id='F4gUv'><style id='F4gUv'><dir id='F4gUv'><q id='F4gUv'></q></dir></style></legend>
          • <bdo id='F4gUv'></bdo><ul id='F4gUv'></ul>
              <i id='F4gUv'><tr id='F4gUv'><dt id='F4gUv'><q id='F4gUv'><span id='F4gUv'><b id='F4gUv'><form id='F4gUv'><ins id='F4gUv'></ins><ul id='F4gUv'></ul><sub id='F4gUv'></sub></form><legend id='F4gUv'></legend><bdo id='F4gUv'><pre id='F4gUv'><center id='F4gUv'></center></pre></bdo></b><th id='F4gUv'></th></span></q></dt></tr></i><div id='F4gUv'><tfoot id='F4gUv'></tfoot><dl id='F4gUv'><fieldset id='F4gUv'></fieldset></dl></div>

                  <tfoot id='F4gUv'></tfoot>

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

                  本文介绍了列“在选择列表中无效,因为它不包含在聚合函数或 GROUP BY 子句中"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想在下面的 SQL 中显示列 B,但是当我将它添加到查询中时,它给了我以下错误:

                  <块引用>

                  T2.B' 列在选择列表中无效,因为它不包含在聚合函数或 GROUP BY 子句.

                  我的代码:

                  SELECT A, COUNT(B) as T1, B从 T2哪里 ID=1按 A 分组

                  解决方案

                  换句话说,这个错误告诉你SQL Server不知道哪个 B从组中选择.

                  无论您想选择一个特定的值(例如 MINSUMAVG),在这种情况下,您都可以使用适当的聚合函数,或者您想选择每个值作为新行(即在 GROUP BY 字段列表中包含 B).

                  <小时>

                  考虑以下数据:

                  <前>身份证号1 1 131 1 791 2 131 2 131 2 42

                  查询

                  SELECT A, COUNT(B) AS T1从 T2按 A 分组

                  会回来:

                  <前>一个 T11 22 3

                  一切都很好.

                  但是,请考虑以下(非法)查询,这会产生此错误:

                  SELECT A, COUNT(B) AS T1, B从 T2按 A 分组

                  及其返回的说明问题的数据集:

                  <前>A T1 B1 2 13?79?13 和 79 作为单独的行?(13+79=92)?……?2 3 13?42?……?

                  <小时>

                  但是,以下两个查询说明了这一点,不会导致错误:

                  1. 使用聚合

                    SELECT A, COUNT(B) AS T1, SUM(B) AS B从 T2按 A 分组

                    会回来:

                    <前>A T1 B1 2 922 3 68

                  2. 将列添加到GROUP BY列表

                    SELECT A, COUNT(B) AS T1, B从 T2按 A、B 分组

                    会回来:

                    <前>A T1 B1 1 131 1 792 2 132 1 42

                  I would like to display the column B in my below SQL, but when I add it to the query it gives me the following error:

                  Column T2.B' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

                  My code:

                  SELECT A, COUNT(B) as T1, B 
                  FROM T2 
                  WHERE ID=1 
                  GROUP BY A 
                  

                  解决方案

                  Put in other words, this error is telling you that SQL Server does not know which B to select from the group.

                  Either you want to select one specific value (e.g. the MIN, SUM, or AVG) in which case you would use the appropriate aggregate function, or you want to select every value as a new row (i.e. including B in the GROUP BY field list).


                  Consider the following data:

                  ID  A   B
                  1   1  13
                  1   1  79
                  1   2  13
                  1   2  13
                  1   2  42
                  

                  The query

                  SELECT A, COUNT(B) AS T1 
                  FROM T2 
                  GROUP BY A
                  

                  would return:

                  A  T1
                  1  2
                  2  3
                  

                  which is all well and good.

                  However consider the following (illegal) query, which would produce this error:

                  SELECT A, COUNT(B) AS T1, B 
                  FROM T2 
                  GROUP BY A
                  

                  And its returned data set illustrating the problem:

                  A  T1  B
                  1  2   13? 79? Both 13 and 79 as separate rows? (13+79=92)? ...?
                  2  3   13? 42? ...?
                  


                  However, the following two queries make this clear, and will not cause the error:

                  1. Using an aggregate

                    SELECT A, COUNT(B) AS T1, SUM(B) AS B
                    FROM T2
                    GROUP BY A
                    

                    would return:

                    A  T1  B
                    1  2   92
                    2  3   68
                    

                  2. Adding the column to the GROUP BY list

                    SELECT A, COUNT(B) AS T1, B
                    FROM T2
                    GROUP BY A, B
                    

                    would return:

                    A  T1  B
                    1  1   13
                    1  1   79
                    2  2   13
                    2  1   42
                    

                  这篇关于列“在选择列表中无效,因为它不包含在聚合函数或 GROUP BY 子句中"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:理解 T-SQL 中的 PIVOT 函数 下一篇:插入多行而不重复“INSERT INTO ...";声明的一部分?

                  相关文章

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

                      <tfoot id='peEb7'></tfoot>
                      • <bdo id='peEb7'></bdo><ul id='peEb7'></ul>
                      <legend id='peEb7'><style id='peEb7'><dir id='peEb7'><q id='peEb7'></q></dir></style></legend>
                    1. <small id='peEb7'></small><noframes id='peEb7'>