SQL Server:在同一个表

时间:2017-04-02 10:50:46

标签: sql sql-server

我有一个Project表,每个项目都有多个员工连接到不同的角色。我希望能够使用多个员工过滤项目,例如我想获得所有项目,其中“Janet”是销售代表,“Mark”是项目经理...这是我到目前为止所尝试的它没有显示预期的结果:

SELECT 
    p_Empl.First_Name, 
    p_Resources.cRole, p_Resources.Project_ID, 
    p_Resources.Resources_ID, p_Resources.Empl_ID, 
    p_Project.Project_ID AS Expr1, 
    p_Project.ProjectNum, 
    p_Empl.Last_Name
FROM  
    p_Project 
LEFT OUTER JOIN
    p_Resources ON p_Project.Project_ID = p_Resources.Project_ID 
LEFT OUTER JOIN
    p_Empl ON p_Resources.Empl_ID = p_Empl.ID
WHERE 
    p_Project.Project_ID IN (SELECT p_Project.Project_ID
                             FROM p_Project 
                             LEFT OUTER JOIN p_Client ON p_Project.Client_ID = p_Client.Client_ID
                             WHERE p_Project.Project_ID IN (SELECT p_Project.Project_ID
                                                            FROM p_Project 
                                                            INNER JOIN p_Resources ON p_Resources.Project_ID = p_Project.Project_ID
                                                            INNER JOIN p_Empl ON p_Resources.Empl_ID = p_Empl.ID
                                                            WHERE p_Empl.First_Name = 'Mark'
                                                              AND p_Resources.cRole = 'Sales Manager'
                                                            ORDER BY p_Project.Project_ID)
                              AND p_Project.Project_ID IN (SELECT p_Project.Project_ID
                                                           FROM p_Project 
                                                           INNER JOIN p_Resources ON p_Resources.Project_ID = p_Project.Project_ID
                                                           INNER JOIN p_Empl ON p_Resources.Empl_ID = p_Empl.ID
                                                           WHERE p_Empl.First_Name = 'Janet'
                                                             AND p_Resources.cRole = 'Project Manager'
                                                          ORDER BY p_Project.Project_ID)
                             GROUP BY 
                                 p_Project.Project_ID, p_Client.ClientName, p_Project.ProjectName, p_Project.ProjectNum
                             ORDER BY 
                                 p_Client.ClientName, p_Project.ProjectName, p_Project.ProjectNum 
                             OFFSET (25 * (1)) ROWS FETCH NEXT (25) ROWS ONLY)

我对SQL和编程都很陌生。如果有人可以帮助我,我真的很感激。感谢。

2 个答案:

答案 0 :(得分:0)

您不需要两个子查询。在不同的过滤器组之间只使用一个OR,并在projectId上使用聚合来使用having子句来确保该项目的两个都存在。

select p_Empl.First_Name,
    p_Resources.cRole,
    p_Resources.Project_ID,
    p_Resources.Resources_ID,
    p_Resources.Empl_ID,
    p_Project.Project_ID as Expr1,
    p_Project.ProjectNum,
    p_Empl.Last_Name
from p_Project
left outer join p_Resources on p_Project.Project_ID = p_Resources.Project_ID
left outer join p_Empl on p_Resources.Empl_ID = p_Empl.ID
where p_Project.Project_ID in (
        select p_Project.Project_ID
        from p_Project
        left outer join p_Client on p_Project.Client_ID = p_Client.Client_ID
        where p_Project.Project_ID in (
                select p_Project.Project_ID
                from p_Project
                inner join p_Resources on p_Resources.Project_ID = p_Project.Project_ID
                inner join p_Empl on p_Resources.Empl_ID = p_Empl.ID
                where (p_Empl.First_Name = 'Mark'
                    and p_Resources.cRole = 'Sales Manager')
                    or (p_Empl.First_Name = 'Janet'
                    and p_Resources.cRole = 'Project Manager')
                group by p_Project.Project_ID
                having count(distinct p_Empl.First_Name + p_Resources.cRole) = 2
                )
        group by p_Project.Project_ID,
            p_Client.ClientName,
            p_Project.ProjectName,
            p_Project.ProjectNum
        order by p_Client.ClientName,
            p_Project.ProjectName,
            p_Project.ProjectNum OFFSET(25 * (1)) ROWS fetch next (25) ROWS only
        )

答案 1 :(得分:0)

如果你想要项目,那么我会使用聚合:

SELECT p.Project_ID , p.ProjectNum
FROM p_Project p JOIN
     p_Resources r
     ON p.Project_ID = r.Project_ID LEFT OUTER JOIN
     p_Empl e
     ON r.Empl_ID = e_Empl.ID
WHERE (e.First_Name = 'Mark' AND r.cRole = 'Sales Manager') OR
      (e.First_Name = 'Janet' AND r.cRole = 'Project Manager')
GROUP BY p.Project_ID , p.ProjectNum
HAVING COUNT(DISTINCT e.First_Name) = 2;

如果你想要项目的所有细节,我会做这样的事情:

WITH per as (
      SELECT . . .   -- all the columns you want
      FROM p_Project p JOIN
           p_Resources r
           ON p.Project_ID = r.Project_ID LEFT OUTER JOIN
           p_Empl e
           ON r.Empl_ID = e_Empl.ID
     )
SELECT per.*
FROM per
WHERE per.Projecdt_Id IN (SELECT per2.ProjectId
                          FROM per per2
                          WHERE (per2.First_Name = 'Mark' AND per2.cRole = 'Sales Manager') OR
                                (per2.First_Name = 'Janet' AND per2.cRole = 'Project Manager')
                          GROUP BY p.Project_ID 
                          HAVING COUNT(DISTINCT e.First_Name) = 2  
                         );