如何投影重复属性

时间:2015-10-26 19:12:15

标签: sql database database-design relational relational-algebra

使用关系代数,我必须检索薪水高于他们所在部门经理薪水的员工名单。

我有以下2个表格:

员工

Fname | Minit | Lname | **SSN** | BDate | Address | Sex | Salary | SupervSSN | DeprtNo

DEPARTMENT

Dname | Dnumber | MgrSSN | MgrStartDate

DeprtNo指向DnumberMgrSSN指向SSN

我尝试此查询的方式如下:

MGRS ← (EMPLOYEE) |><| MgrSSN = SSN (DEPARTMENT) //All Manager employees
EMPS ← (EMPLOYEE) |><| MgrSSN != SSN (DEPARTMENT)//All non-managers employees
EM   ← (EMPS) |><| DeprtNo = DeprtNo (MGRS)      //Managers are joined with non-managers who work in their Department.

现在我想根据一个将经理的工资与非经理的工资进行比较的选择项目来管理经理SSN。但是,EM包含重复类别 - SSN和Salary。一次为经理,一次为非经理。所以我不知道如何正确投射。

1 个答案:

答案 0 :(得分:0)

select MgrSSN from Department where 
Dnumber in (
  select DeprtNo from Employee where
  SSN not in (select Employee.SSN  from Employee inner join Department on Employee.SSN = Department.MgrSSN)
  and Salary > (select min(Employee.Salary) from Employee inner join Department on Employee.SSN = Department.MgrSSN)
)

我认为这应该可以预测那些存在薪水高于经理的员工的部门的经理。