在 SQL 查询中检索属性时遇到问题
Trouble retrieving an attribute in SQL query
我有以下关系模式。
雇员(Fname, Lname, Ssn, Salary, Dno)
部门(Dname, Dnumber, Mgr_ssn)
对于每个部门,我想检索部门编号、经理名字和部门平均工资。
以下是我的尝试。我得到了部门编号和平均工资,但我无法理解如何在不过滤会更改平均工资值的行的情况下检索部门经理姓名。
SELECT D.Dnumber, AVG(E.Salary)
FROM EMPLOYEE E JOIN DEPARTMENT D ON E.Dno = D.Dnumber
GROUP BY D.Dnumber
如何在此查询中检索经理的名字?
据推测,给经理的 link 是通过 SSN。对我来说,将 PII(个人身份信息)用于此类目的是个坏主意。
无论如何,你需要 两个 加入才能获得名字,所以你已经完成了一半:
select d.dnumber, avg(e.salary), em.fname
from employee e join
department d
on e.dno = d.dnumber left join
employee em
on e.ssn = em.mgr_ssn
group by d.dnumber, em.fname;
我有以下关系模式。
雇员(Fname, Lname, Ssn, Salary, Dno)
部门(Dname, Dnumber, Mgr_ssn)
对于每个部门,我想检索部门编号、经理名字和部门平均工资。
以下是我的尝试。我得到了部门编号和平均工资,但我无法理解如何在不过滤会更改平均工资值的行的情况下检索部门经理姓名。
SELECT D.Dnumber, AVG(E.Salary)
FROM EMPLOYEE E JOIN DEPARTMENT D ON E.Dno = D.Dnumber
GROUP BY D.Dnumber
如何在此查询中检索经理的名字?
据推测,给经理的 link 是通过 SSN。对我来说,将 PII(个人身份信息)用于此类目的是个坏主意。
无论如何,你需要 两个 加入才能获得名字,所以你已经完成了一半:
select d.dnumber, avg(e.salary), em.fname
from employee e join
department d
on e.dno = d.dnumber left join
employee em
on e.ssn = em.mgr_ssn
group by d.dnumber, em.fname;