我有以下SQL语句来检索项目名称,项目经理和项目状态:
目前,当我将团队成员分配给项目时,它们会从Member表写入ProjectIterationMember表。我想做的是计算该特定项目的团队成员数量。我需要在这个SQL语句中使用它,因为我在datagridview中显示所有内容。谢谢你的帮助!
表格结构:
Project {ProjectID, CompanyID, ProjectName, StartDate, EndDate, Description}
Iterations {ProjectIterationID, ProjectID, StartDate, EndDate}
Member {MemberID, FirstName, LastName, CompanyRole}
ProjectIterationMember {ProjectIterationMemberID, ProjectIterationID, MemberID}
SELECT DISTINCT(Project.ProjectName),
Project.ProjectID, Project.Status,
Project.CompanyID, Project.StartDate,
m.MemberID, m.FirstName + ' ' + m.LastName AS
ProjectManager
FROM
Project, Member m, ProjectIterationMember, Iterations i
WHERE
m.CompanyRole = 'Project Manager' AND Project.CompanyID = '" + co_id + "' AND
m.MemberID = ProjectIterationMember.MemberID AND i.ProjectIterationID =
ProjectIterationMember.ProjectIterationID AND i.ProjectID = Project.ProjectID ORDER BY
Project.StartDate DESC
答案 0 :(得分:0)
试试这个:
SELECT YOUR_SELECT_HERE, count(m.MemberID) as MemberCount
FROM YOUR_FROM_HERE
WHERE YOUR_WHERE_HERE
GROUP BY m.MemberID
答案 1 :(得分:0)
基本上有两种方法可以做到这一点。
首先:将分组添加到该语句。根据您的数据,这可能有效,也可能无效。正如我所看到的,您目前正加入ProjectIterationMember,前往Projekt经理。要使用分组,您可以第二次为所有其他成员加入。此外,通过分组,您不需要使用distinct。
SELECT Project.ProjectName,
Project.ProjectID, Project.Status,
Project.CompanyID, Project.StartDate,
m.MemberID, m.FirstName + ' ' + m.LastName AS
ProjectManager,
Count(allMembers.MemberID) AS MemberCount
FROM
Project, Member m, ProjectIterationMember, Iterations i, ProjectIterationMember allMembers
WHERE
m.CompanyRole = 'Project Manager' AND Project.CompanyID = '" + co_id + "' AND
m.MemberID = ProjectIterationMember.MemberID AND i.ProjectIterationID =
ProjectIterationMember.ProjectIterationID AND i.ProjectID = Project.ProjectID AND i.ProjectIterationID =
allMembers.ProjectIterationID
GROUP BY Project.ProjectName,
Project.ProjectID, Project.Status,
Project.CompanyID, Project.StartDate,
m.MemberID, m.FirstName, m.LastName
ORDER BY
Project.StartDate DESC
第二种方法是内部选择。例如,如果您的数据结构不支持此选择中的分组,则需要这样做。您还可以将内部选择导出到用户定义的函数,并在其他地方重复使用。
SELECT DISTINCT(Project.ProjectName),
Project.ProjectID, Project.Status,
Project.CompanyID, Project.StartDate,
m.MemberID, m.FirstName + ' ' + m.LastName AS
ProjectManager,
(SELECT COUNT(MemberID) FROM ProjectIterationMember WHERE ProjectIterationID = i.ProjectIterationID) AS MemberCount
FROM
Project, Member m, ProjectIterationMember, Iterations i
WHERE
m.CompanyRole = 'Project Manager' AND Project.CompanyID = '" + co_id + "' AND
m.MemberID = ProjectIterationMember.MemberID AND i.ProjectIterationID =
ProjectIterationMember.ProjectIterationID AND i.ProjectID = Project.ProjectID ORDER BY
Project.StartDate DESC