获取与 GROUP BY 列相关的列

发布于 2024-12-03 01:27:05 字数 644 浏览 0 评论 0原文

我想做如下的事情。

SELECT aspnet_Users.UserName, aspnet_Membership.Email, count(*) as Activities
FROM aspnet_Users
     INNER JOIN Activities ON aspnet_Users.UserId = Activities.ActUserID
     INNER JOIN aspnet_Membership ON aspnet_Users.UserId = aspnet_Membership.UserId
     WHERE Activities.ActDateTime >= GETDATE()
     GROUP BY aspnet_Users.UserName
     ORDER BY Activities DESC

但这给了我一个错误。

列“aspnet_Membership.Email”在选择列表中无效,因为它未包含在聚合函数或 GROUP BY 子句中。

我有点理解这个错误。我正在尝试选择不属于分组的列。

但是,aspnet_Membership.Email 和 aspnet_Users.UserId 之间始终存在一对一的关系。那么我该如何实现呢?

I'd like to do something like the following.

SELECT aspnet_Users.UserName, aspnet_Membership.Email, count(*) as Activities
FROM aspnet_Users
     INNER JOIN Activities ON aspnet_Users.UserId = Activities.ActUserID
     INNER JOIN aspnet_Membership ON aspnet_Users.UserId = aspnet_Membership.UserId
     WHERE Activities.ActDateTime >= GETDATE()
     GROUP BY aspnet_Users.UserName
     ORDER BY Activities DESC

But this gives me an error.

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

I understand the error somewhat. I'm trying to select a column that is not part of the grouping.

However, there will always be a one-to-one relationship between aspnet_Membership.Email and aspnet_Users.UserId. So how would I implement this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

此生挚爱伱 2024-12-10 01:27:05

更改:

GROUP BY aspnet_Users.UserId

至:

GROUP BY aspnet_Users.UserName, aspnet_Membership.Email

不确定为什么您认为如果您不想返回 UserId 列,则需要在分组中提及它,或者为什么您认为不应该按您所做的列进行分组想回来。

Change:

GROUP BY aspnet_Users.UserId

To:

GROUP BY aspnet_Users.UserName, aspnet_Membership.Email

Not sure why you think you need to mention the UserId column in the grouping if you don't want to return it, or why you think you shouldn't group by the columns you do want to return.

水波映月 2024-12-10 01:27:05

要选择一个列,它必须位于 group by 子句或聚合中,您可以考虑按 (aspnet_Users.UserName, aspnet_Membership.Email,aspnet_Users.UserId ) 进行分组。

我猜它会起作用

to select a column it must either be in a group by clause or aggregated ,you could consider grouping by (aspnet_Users.UserName, aspnet_Membership.Email,aspnet_Users.UserId ) .

my guess is it would work

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