每个 GROUP BY 表达式必须至少包含一列不是外部引用
我在这里做错了什么?我收到此错误:
SELECT LEFT(SUBSTRING(batchinfo.datapath, PATINDEX('%[0-9][0-9][0-9]%', batchinfo.datapath), 8000),
PATINDEX('%[^0-9]%', SUBSTRING(batchinfo.datapath, PATINDEX('%[0-9][0-9][0-9]%',
batchinfo.datapath), 8000))-1),
qvalues.name,
qvalues.compound,
qvalues.rid
FROM batchinfo JOIN qvalues ON batchinfo.rowid=qvalues.rowid
WHERE LEN(datapath)>4
GROUP BY 1,2,3
HAVING rid!=MAX(rid)
我想按具有最大删除值的第一、第二和第三列进行分组。
无需 group by 和having 即可正常工作。
What am I doing wrong here? I am getting this error on:
SELECT LEFT(SUBSTRING(batchinfo.datapath, PATINDEX('%[0-9][0-9][0-9]%', batchinfo.datapath), 8000),
PATINDEX('%[^0-9]%', SUBSTRING(batchinfo.datapath, PATINDEX('%[0-9][0-9][0-9]%',
batchinfo.datapath), 8000))-1),
qvalues.name,
qvalues.compound,
qvalues.rid
FROM batchinfo JOIN qvalues ON batchinfo.rowid=qvalues.rowid
WHERE LEN(datapath)>4
GROUP BY 1,2,3
HAVING rid!=MAX(rid)
I would like to group by the first, second, and third columns having the max rid.
It works fine without the group by and having.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
首先,您不能这样做:
HAVING 子句只能包含属于聚合组属性的内容。
另外,
1, 2, 3
在 SQL Server 的 GROUP BY 中无效 - 我认为这仅在 ORDER BY 中有效。您能解释一下为什么这不是您要找的吗:
To start with you can't do this:
The HAVING clause can only contain things which are attributes of the aggregate groups.
In addition,
1, 2, 3
is not valid in GROUP BY in SQL Server - I think that's only valid in ORDER BY.Can you explain why this isn't what you are looking for:
好吧,正如之前所说,你不能按文字进行
GROUP
,我认为你很困惑,因为你可以ORDER
按1、2、3。当你使用函数作为您的列,您需要按相同的表达式进行分组。此外,HAVING子句是错误的,你只能使用聚合中的内容。在这种情况下,您的查询应该如下所示:Well, as it was said before, you can't
GROUP
by literals, I think that you are confused cause you canORDER
by 1, 2, 3. When you use functions as your columns, you need to GROUP by the same expression. Besides, the HAVING clause is wrong, you can only use what is in the agreggations. In this case, your query should be like this:您不能按文字分组,只能按列分组。
您可能正在寻找这样的内容:
首先,您必须为第一个表达式指定一个带有
as
的列名称。然后,您必须在 group by 表达式中指定列的名称。You can't group by literals, only columns.
You are probably looking for something like this:
First of all, you have to give that first expression a column name with
as
. Then you have to specify the names of the columns in the group by expression.当我在 SELECT 中有一个“固定”值列时,我收到了此错误消息,并且作为一个好孩子,我也在 GROUP BY 中使用了它。
例如:
修复:
固定的
'Person'
值毫无意义,因为它在每一行中都是相同的值,并且没有任何内容可以与该值分组。或者这是我对错误的解释。我使用这个固定值列的原因是因为我正在准备查询以在数据透视表中使用它。
I had this error message when i had a "fixed" value column in SELECT and as a good boy, i used it in the GROUP BY too.
For example:
the fix:
The fixed
'Person'
value is pointless since it's the same value throughout every row and there is nothing to group by with this value. Or this is my interpretation of the error.The reason why i use this fixed value column, is because i was preparing queries to use it in a Pivot table.
我认为您没有正确使用 GROUP BY 。
GROUP BY 的要点是在执行数学/聚合函数之前根据特定列将表组织为多个部分。
例如,在此表中:
SELECT 语句可以按姓名(Bob、Sally 和 John 各自为单独的组)、年龄(Bob 为一组,Sally 和 John 为另一组)或薪水(几乎相同)进行 GROUP BY结果作为名称)。
按“1”分组没有任何意义,因为“1”不是列名。
I think you're not using GROUP BY properly.
The point of GROUP BY is to organize your table into sections based off a certain column or columns before performing math/aggregate functions.
For example, in this table:
A SELECT statement could GROUP BY name (Bob, Sally, and John would each be separate groups), Age (Bob would be one group, Sally and John would be another), or Salary (pretty much same result as name).
Grouping by "1" doesn't make any sense because "1" is not a column name.
我刚刚在选择查询的 group by 子句中使用 GETDATE() [即外部引用] 时发现了此错误。
当将其替换为相应表中的日期列时,它会被清除。
认为作为一个简单的例子来分享。干杯;)
I just found this error., while using GETDATE() [i.e outer reference] in the group by clause in a select query.
When replaced it with date column from the respective table it cleared.
Thought to share as a simple example. cheers ;)
当您使用
GROUP BY
时,您还需要对不在group by子句内的列使用聚合函数。我不知道你到底想做什么,但我想这会起作用:
编辑:
我在这里尝试做的是一个
group by
,其中包含除rid
之外的所有字段。如果这不是您想要的,为了获得有效的 SQL 语句,您需要做的是为每个删除的 group by 字段添加聚合函数调用...When you're using
GROUP BY
, you need to also use aggregate functions for the columns not inside your group by clause.I don't know exactly what you're trying to do, but I guess this would work:
Edit:
What I'm trying to do here is a
group by
with all fields butrid
. If that's not what you want, what you need to do in order to have a valid SQL statement is adding an aggregate function call for each removed group by field...下面是一个简单的查询,用于查找药品类型为 A 且产量超过 2 的公司名称。
Here's a simple query to find company name who has a medicine type of A and makes more than 2.
我因为一个愚蠢的错误而收到此错误 - 我的
GROUP BY
列用引号引起来!错误
正确
信用:@SOound的答案让我想到了这一点出去
I got this error because of a silly mistake - I had my
GROUP BY
column in quotes!Wrong
Correct
Credit: @SOund's answer led me to figure this out