Displaying the same fields AS different names from

2019-07-24 15:20发布

I have EmployeeName; it is from the Employee table. The Employee table holds ALL Employees in the organization and the Employee table references the primary key of the Position table, which holds the different position names. So this is how I'm differentiating between employees in the Employee table; each record in the Employee table has a PosNo which references the Position table(worker = Pos1, manager = Pos2, etc...) So for simplicity's sake, a record in the employee table would be similar to: EmployeeName, EmployeeAddress, DeptNo, PosNo

Here's the problem: Certain positions are under other positions. There are workers in the Employee table and there are managers in the Employee table. I'm wanting to make a table that lists all workers and their managers. For example, the table would have two fields: EmployeeName, ManagerName.

The Employee table is broken down into a generalization hierarchy. The Salary and Hourly tables branch out from Employee table. Then, from the Salary table, another table branches out called Manager(which I call ProgramSupervisor; it has a unique field). Workers are part of the Hourly table though. Managers(ProgramSupervisor) and Workers(Hourly) are related to each other through the ISL table. The Manager is the head of the ISL and therefore ISL has a ManagerNo as one of its fields. Workers(Hourly), however, work in the ISL and therefore have ISLNo as a field in their table, the Hourly table.

So, I'm trying to find a way to relate all of these table as make a table with two fields, workers and managers, in which the workers belong to managers through the ISL table. Would I use a nested query of some sort? I'll post my code so far, which is absolutely not correct (probably no even on the right track) and I'll post my ERD so you can get a better picture of how the tables relate.

 SELECT  EmpLastName + ', ' + EmpFirstName as ProgSupName, 
          EmpLastName + ', ' + EmpFirstName as EmpName
 FROM Employee, Salary, ProgramSupervisor, ISL, Hourly
 WHERE Employee.EmpNo = Salary.EmpNo
 AND Salary.EmpNo = ProgramSupervisor.EmpNo
 AND ProgramSupervisor.EmpNo = ISL.ProgramSupervisor_EmpNo
 AND ISL.ISLNo = Hourly.ISLNo
 AND Hourly.EmpNo = Employee.EmpNo

ERD

1条回答
闹够了就滚
2楼-- · 2019-07-24 16:13

In its simplest form you can distill the Employee-Supervisor relationship down to three tables:

[Employee]

EmpNo  EmpFirstName  EmpLastName
-----  ------------  -----------
    1  Montgomery    Burns      
    2  Homer         Simpson    

[Hourly]

EmpNo  ISLNo
-----  -----
    2      1

[ISL]

ISLNo  ProgramSupervisor_EmpNo  ISLName                  
-----  -----------------------  -------------------------
    1                        1  Springfield Nuclear Plant

If you put them together in a query that looks like this

Query.png

it produces results like this:

Employee_LastName  Employee_FirstName  ISLName                    Supervisor_LastName  Supervisor_FirstName
-----------------  ------------------  -------------------------  -------------------  --------------------
Simpson            Homer               Springfield Nuclear Plant  Burns                Montgomery          

"But wait a minute!" I hear you say, "There are four tables in that query. Where did the [Supervisor] table come from?"

That is just another instance of the [Employee] table that uses [Supervisor] as its alias. A table can appear in a query more than once provided that we use aliases to specify the instance to which we are referring when we talk about [EmpLastName], [EmpFirstName], etc..

The SQL for the above query shows the second instance Employee AS Supervisor on the second-last line:

SELECT 
    Employee.EmpLastName AS Employee_LastName, 
    Employee.EmpFirstName AS Employee_FirstName, 
    ISL.ISLName, 
    Supervisor.EmpLastName AS Supervisor_LastName, 
    Supervisor.EmpFirstName AS Supervisor_FirstName
FROM 
    (
        Employee 
        INNER JOIN 
        (
            Hourly 
            INNER JOIN 
            ISL 
                ON Hourly.ISLNo = ISL.ISLNo
        ) 
            ON Employee.EmpNo = Hourly.EmpNo
    ) 
    INNER JOIN 
    Employee AS Supervisor 
        ON ISL.ProgramSupervisor_EmpNo = Supervisor.EmpNo
查看更多
登录 后发表回答