我可以在 SQL 的聚合函数中包含非聚合列而不将其放入 GROUP BY 子句中吗?
看看下表...
Classes
ClassId ClassName
1 Math
2 Math
3 Science
4 Music
Registrations
RegistrationId ClassId StudentName
1 1 Stu
2 1 Rick
3 2 John
4 4 Barb
5 4 Dan
6 3 Einstein
是的,有 2 个班级具有相同的名称(数学),因为它们可能在不同的时间。我想获得一份课程列表以及每个课程注册的学生人数。我想要以下列(ClassId、ClassName、StudentCount)。
我对此的尝试将类似于......
SELECT Classes.ClassId, Classes.ClassName, Count(Registrations.RegistrationId)
FROM Classes
INNER JOIN Registrations ON Classes.ClassId = Registrations.ClassId
GROUP BY Classes.ClassId
(注意我想按 ClassId 分组,而不是 ClassName)。这在 SQLServer 2008 中可能吗?显然我问是因为 SQL 抱怨
"ClassName is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."
谢谢!
Take the following tables...
Classes
ClassId ClassName
1 Math
2 Math
3 Science
4 Music
Registrations
RegistrationId ClassId StudentName
1 1 Stu
2 1 Rick
3 2 John
4 4 Barb
5 4 Dan
6 3 Einstein
Yes, there are 2 classes with the same name (Math) as they may be at different times. I would like to get a list of the classes and the number of students registered for each one. I would like the following columns (ClassId, ClassName, StudentCount).
My attempt at this would be something along the lines of...
SELECT Classes.ClassId, Classes.ClassName, Count(Registrations.RegistrationId)
FROM Classes
INNER JOIN Registrations ON Classes.ClassId = Registrations.ClassId
GROUP BY Classes.ClassId
(Note I would like to GroupBy the ClassId but NOT ClassName). Is this possible in SQLServer 2008? Obviously I ask because SQL complains
"ClassName is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不可以,SQL Server 不允许您省略 GROUP BY 中未包装在聚合函数中的列。包含类名没有什么坏处,因为分组依据将在分组依据列的组合上执行:
您可以使用以下方法根据计数派生一个表:
No, SQL Server does not allow you to omit columns from the GROUP BY that are not wrapped in aggregate functions. There's no harm in including the class name, because the group by will be performed on the combination of the group by columns:
You could derive a table based on the counting, using:
在 GROUP BY 语句中包含 Classes.ClassName 应该没有什么坏处。您将按不同的 ClassId 和 ClassName 对进行分组,因此 (1, 'Math') 和 (2, 'Math') 仍然是两个不同的分组。
There should be no harm in including Classes.ClassName in your GROUP BY statement. You'd be grouping by the distinct pairs of ClassId and ClassName so (1, 'Math') and (2, 'Math') are still two distinct groupings.
您可以将 ClassName 放在 group by 子句中,这样就可以了,因为它与 ClassID 是一对一的:
SELECT Classes.ClassId, Classes.ClassName, Count(Registrations.RegistrationId)
来自班级
INNER JOIN 注册 ON Classes.ClassId = Registrations.ClassId
GROUP BY Classes.ClassId、Classes.ClassName
或在 select 子句中放置 MAX(ClassName)。任何一种都会产生相同的结果。
You can either put the ClassName in the group by clause, which will be ok because it is a 1-to1 with the ClassID:
SELECT Classes.ClassId, Classes.ClassName, Count(Registrations.RegistrationId)
FROM Classes
INNER JOIN Registrations ON Classes.ClassId = Registrations.ClassId
GROUP BY Classes.ClassId, Classes.ClassName
or put a MAX(ClassName) in the select clause. Either one will yield the same result.
不,你不能:这是一个矛盾。
GROUP BY = 折叠为离散值。如果不崩溃,就需要聚合它。
碰巧的是,无论如何您都会得到相同的结果,因为 ClassName 取决于 ClassID。
No, you can't: it's a contradiction.
GROUP BY = collapse to discrete values. If you don't collapse, you need to aggregate it.
As it happens, you'd get the same result anyway because ClassName depends on ClassID.