error using "order by" in a select statement (error: column is not contained in "either an aggregate function or the GROUP BY clause")

error using "order by" in a select statement (error: column is not contained in "either an aggregate function or the GROUP BY clause")

我下面有一个table,想统计每个字母连续出现的次数。重现我正在使用的 table 的代码已列出,以帮助节省时间。

CREATE TABLE table1 (id integer, names varchar(50));

  INSERT INTO table1 VALUES (1,'A');
  INSERT INTO table1 VALUES (2,'A');
  INSERT INTO table1 VALUES (3,'B');
  INSERT INTO table1 VALUES (4,'B');
  INSERT INTO table1 VALUES (5,'B');
  INSERT INTO table1 VALUES (6,'B');
  INSERT INTO table1 VALUES (7,'C');
  INSERT INTO table1 VALUES (8,'B');
  INSERT INTO table1 VALUES (9,'B');

  select * from table1; 

我发现已经编写了代码来在线完成此操作,我已经对其进行了测试并且可以确认它 运行s 成功。它显示在这里。

select names, count(*) as count
from (select id, names, (row_number() over (order by id) - row_number() over (partition by names order by id)) as grp
      from table1
     ) as temp
group by grp, names

我试图在末尾添加 ORDER BY 子句,如下所示:

select names, count(*) as count
from (select id, names, (row_number() over (order by id) - row_number() over (partition by names order by id)) as grp
      from table1
     ) as temp
group by grp, names
order by id -- added this here, but it creates an error.

但一直收到错误 "Column "temp.id“在 ORDER BY 子句中无效,因为它既未包含在聚合函数中,也未包含在 GROUP BY 子句中。”但是,我可以通过 "names." 来订购,这里有什么区别?

另外,为什么我不能在子查询中添加"order by id"?如果我 运行 这个子查询本身(见下文),那么 "order by id" 没问题,但总的来说它不能 运行。为什么是这样?

select names, count(*) as count
from (select id, names, (row_number() over (order by id) - row_number() over (partition by names order by id)) as grp
      from table1
      order by id -- added this in here, but it creates an error. 
     ) as temp
group by grp, names
order by names

一个 select 语句 returns 行,任意顺序——除非它有一个 order by。这是对 无序 集上的 SQL 运算符这一事实的扩展。

您的 select 没有 order by,因此您不应假设数据会以任何特定顺序返回。要按 id 获取结果顺序,请将 order by id 添加到 select.

kept getting the error "Column "temp.id" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause." However, I am able to order by "names." What is the difference here?

SQL 按照一定的顺序做事。如果您的查询有 GROUP BY(您的查询有),则首先完成。分组后,SQL 唯一具有的是选择和分组依据的列,因此这些是唯一可以在 order by 子句中使用的列。

举个例子,想想街道上的房子。如果您查询房屋,返回颜色和数量,您可能会得到类似红色 2、白色 10、绿色 3 的信息。但是要求按地址编号排序是没有意义的,因为该信息不在我们返回的数据中.在您的情况下,您返回的是名称、计数,并且您在 group by 子句中使用了 grp,所以这些是您唯一可以用来对最终数据进行排序的东西,因为它们就是您所拥有的,所有这些都是有意义的。

Also, why can't I add in the "order by id" in the subquery? If I run this subquery on its own (see below), then the "order by id" is fine, but all together it cannot run. Why is this?

当您有子查询时,结果的使用就好像它们是 table。你可以加入它,或者像你一样从它查询,但关键是 table 的顺序对其他任何事情都没有影响。基础 table 的输入顺序不能保证您的查询将按该顺序出现,除非您使用 order by 子句。而且因为您正在做一个分组,所以该顺序无论如何都没有任何意义。因为子查询的顺序没有影响,所以SQL不会让你放进去的。