MAX团队成员人数

时间:2011-02-22 00:29:41

标签: sql

好的,所以我正在处理这个凌乱的SQL语句。

我要做的是,计算项目团队成员的数量(包括项目经理)。创建项目详细信息时,Members表中的成员将写入ProjectIterationMembers表。项目可以包含1次或多次迭代。

现在正在发生的事情是,当我添加第二次迭代并将相同的人分配给项目时,我得到两组数据 - Project name, 5 .... Project name, 10。如何确保仅显示最大成员数?提前谢谢!

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 IN ('Project Manager', 'Admin') 
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

2 个答案:

答案 0 :(得分:2)

您需要两次加入Iterations表,一次在子查询内部以及外部,并加入ProjectID而不是ProjectIterationID的内部和外部查询。

如果迭代在每次迭代时都不能有单独的项目管理器,那么您还可以重构外部Iterations联接以删除重复的项目行。以下内容仍然会为您提供重复的Project行,可能还有不同的项目经理,但每个MemberCount都是相同的。

SELECT 
    DISTINCT(P.ProjectName), 
    P.ProjectID, 
    P.Status, 
    P.CompanyID, 
    P.StartDate,   
    M.MemberID,
    M.FirstName + ' ' + M.LastName AS ProjectManager, 
    (   SELECT  COUNT(PIM2.MemberID) 
        FROM    ProjectIterationMember PIM2,
                Iterations I2
        WHERE   I2.ProjectID = P.ProjectID
          AND   PIM2.PIM.ProjectIterationID = I2.ProjectIterationID
    ) AS MemberCount 
FROM 
   Project P, 
   Member M, 
   ProjectIterationMember PIM, 
   Iterations I
WHERE 
        M.CompanyRole IN ('Project Manager', 'Admin') 
    AND P.CompanyID = '" + co_id + "'
    AND M.MemberID = PIM.MemberID 
    AND I.ProjectIterationID = PIM.ProjectIterationID 
    AND I.ProjectID = P.ProjectID 
ORDER BY 
    P.StartDate DESC

答案 1 :(得分:2)

一些注意事项:

  1. 不要使用逗号分隔表的语法。而是使用ISO Join语法。
  2. 我不知道Distinct( Project.ProjectName )会做什么的最模糊的想法。据我所知,任何数据库系统中的语法都是无效的。
  3. 如果同一个项目有多个迭代,哪个经理应该显示?我假设迭代的成员数最多。
  4. 哪个数据库产品和版本?鉴于你没有说,我将假设SQL Server 2005 +
  5. With CountByIteration As
        (
        Select PIM1.ProjectId, I1.ProjectIterationId
            , Count(*) As MemberCount
        From Iterations As I1
            Join ProjectIterationMembers As PIM1
                On PIM1.ProjectIterationId = I1.ProjectIterationId
        Group By PIM1.ProjectId, I1.ProjectIterationId
        )
        , RankedIterations As
        (
        Select ProjectId, ProjectIterationId    
            , Row_Number() Over( Partition By I1.ProjectId Order By MemberCount Desc ) As Rnk
        From CountByIteration
        )
    Select  P.ProjectName, P.ProjectId, P.Status, P.StartDate
        , M.MemberId
        , M.FirstName + ' ' + M.LastName As ProjectManager
        , IterationMemberCounts.MemberCount
    From Project As P
        Join RankedIterations As IterationMemberCounts
            On IterationMemberCounts.ProjectId = P.ProjectId
                And IterationMemberCounts.Rnk = 1
        Join ProjectIterationMembers As PIM
            On PIM.ProjectIterationId = IterationMemberCounts.ProjectIterationId
        Join Members As M
            On M.MemberId = PIM.MemberId
                And M.CompanyRole In( 'Project Manager', 'Admin' )

    现在,您似乎不希望任意选择具有最高成员数的迭代。相反,选择最后一次迭代会更有意义。为此,我们需要一个可以安全地用于指示序列的列(即,不是标识列)。如果在Iterations表上有一个名为CreateDate的列,那么我们只需要更改排名的确定方式:

    , Row_Number() Over( Partition By I1.ProjectId Order By CreateDate Desc ) As Rnk