我可以在 SQL 的聚合函数中包含非聚合列而不将其放入 GROUP BY 子句中吗?

发布于 2024-10-14 12:30:10 字数 929 浏览 5 评论 0原文

看看下表...

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

濫情▎り 2024-10-21 12:30:10

不可以,SQL Server 不允许您省略 GROUP BY 中未包装在聚合函数中的列。包含类名没有什么坏处,因为分组依据将在分组依据列的组合上执行:

  SELECT c.classid, 
         c.classname, 
         COUNT(r.registrationid)
    FROM CLASSES c
    JOIN REGISTRATIONS r ON r.classid = c.classid
GROUP BY c.classid, c.classname

您可以使用以下方法根据计数派生一个表:

  SELECT c.classid, 
         c.classname, 
         r.num
    FROM CLASSES c
    JOIN (SELECT t.classid,
                 COUNT(*) AS num
            FROM REGISTRATIONS t
        GROUP BY t.classid) r ON r.classid = c.classid

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:

  SELECT c.classid, 
         c.classname, 
         COUNT(r.registrationid)
    FROM CLASSES c
    JOIN REGISTRATIONS r ON r.classid = c.classid
GROUP BY c.classid, c.classname

You could derive a table based on the counting, using:

  SELECT c.classid, 
         c.classname, 
         r.num
    FROM CLASSES c
    JOIN (SELECT t.classid,
                 COUNT(*) AS num
            FROM REGISTRATIONS t
        GROUP BY t.classid) r ON r.classid = c.classid
八巷 2024-10-21 12:30:10

在 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.

独守阴晴ぅ圆缺 2024-10-21 12:30:10

您可以将 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.

温柔少女心 2024-10-21 12:30:10

不,你不能:这是一个矛盾。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文