SQL查询以获取在某个位置的所有项目中工作的员工列表

时间:2016-09-21 04:02:21

标签: sql sql-server

表格结构:

  • Employee(emp_id,emp_name,emp_age)
  • ProjectDetails(projid,p_name,p_location)
  • EmpLoyeeProject(emp_no,project_num,hrs)

对于员工投资,这种关系是多对多的。

是否可以帮助编写一个SQL查询,以便为位于“OneLocation”中的所有项目的员工获取eidename

4 个答案:

答案 0 :(得分:1)

如果我正确理解了您的架构,SQL查询可能如下所示:

select distinct emp_id, emp_name from Emplyee e
        inner join EmpLoyeeProject ep on ep.emp_no=e.emp_id
        inner join ProjectDetails p on p.projid=ep.project_num
    where p_location=?

答案 1 :(得分:0)

 SELECT e.emp_id, e.emp_name
    FROM Emplyee e
    INNER JOIN EmpLoyeeProject ep 
    ON e.emp_id = ep.emp_no
    AND ep.project_num IN (
        SELECT DISTINCT ep.project_num 
        FROM EmpLoyeeProject epr 
        INNER JOIN ProjectDetails ep
        ON epr.project_num = p.projid
        WHERE p_location = 'OneLocation'
    )

答案 2 :(得分:0)

我认为您可以使用内部联接检索项目中涉及的所有员工列表,如下所示:

SELECT e.emp_id, e.emp_name
FROM Emplyee e
INNER JOIN EmpLoyeeProject ep ON ep.emp_no = e.emp_id
INNER JOIN ProjectDetails pd ON pd.projid = ep.project_num
    AND pd.p_location = 'youLocation'

答案 3 :(得分:0)

您可以尝试将此作为一种解决方案,也可以选择多个位置。

这是查询所需的最低要求,

SELECT
     e.emp_id
,    e.emp_name
FROM
     employee         e
JOIN employeeproject  ep on e.emp_id = ep.emp_no          -- assuming that ep.emp_no and e.emp_id are the same value (most likely the case)
join projectdetails   pd on ep.projid = pd.project_number -- assuming that these are the same value.
WHERE
    p_location in ('Melbourne') -- set your location , you can also go ('melbourne','sydney','adelaide')

但是我会稍微扩展它以提供更多的数据,以使它对于谁曾要求获取信息更有意义。

SELECT
     e.emp_id
,    e.emp_name
,    pd.p_location
,    p_name
FROM
     employee         e
JOIN employeeproject  ep on e.emp_id = ep.emp_no          -- assuming that ep.emp_no and e.emp_id are the same value (most likely the case)
join projectdetails   pd on ep.projid = pd.project_number -- assuming that these are the same value.
WHERE
    p_location in ('Melbourne') -- set your location , you can also go ('melbourne','sydney','adelaide')

如果您使用像SSRS这样的报告服务,那么您可以做一些很酷的事情,例如根据位置传递参数

您可以将类似于下面的下拉框设置为使其更加用户友好(这个用户可以将数据限制为他们希望查看的特定月份/月份。

enter image description here