gpt4 book ai didi

SQL View如何分组和求和

转载 作者:行者123 更新时间:2023-12-05 04:17:20 26 4
gpt4 key购买 nike

我有一个条目表和一个成员表,我想根据成员名称对所有条目求和。我已经创建了一个 View 来执行此操作,但我在尝试使语法正确时遇到了麻烦。

CREATE VIEW [dbo].[Members_View] AS
SELECT Members.ID, Members.Name, Members.Email,
(SELECT COUNT(*) WHERE AssignedTo = Members.Name) as ECount
From Members JOIN dbo.Entries ON Members.[Name] = Entries.[AssignedTo]
Group By
Name,
Members.ID,
Members.Email,
Entries.AssignedTo

如果我删除分组依据,我只需在新的 ECount 列中为每个条目获得数字 1,但每个名称的倍数。一旦我分组,我每个名字只有一个,但每个条目仍然只有 1 个计数。如何分组并求和?

最佳答案

我想你可以做一个简单的聚合查询,省略 Entries.AssignedTo:

CREATE VIEW [dbo].[Members_View] AS
SELECT Members.ID, Members.Name, Members.Email,
COUNT(*) as ECount
From Members JOIN
dbo.Entries
ON Members.[Name] = Entries.[AssignedTo]
Group By
Name,
Members.ID,
Members.Email;

关于SQL View如何分组和求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23480511/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com