我在这里的第一篇文章,希望得到一些帮助。我需要创建一份报告,显示从未退休的员工。我需要从3个表中提取信息。如果员工有特定扣减代码(7001至7006),则已支付退休金。我有以下表格和列。
Employee
Employee.empid
Employee.fname
Employee.lname
Employee.jobclass
EarnHistory
EarnHistory.empid
EarnHistory.hoursworked
EarnHistory.checknumber
EarnHistory.checkdate
DeductionHistory
DeductionHistory.empid
DeductionHistory.deductioncode
DeductionHistory.checknumber
DeductionHistory.checkdate
查询需要提取empid,fname,lname,hoursworked,deductioncode 如果员工从未支付扣除代码7001至7006。
我尝试过使用NOT EXISTS
失败了答案 0 :(得分:0)
这样的事情应该有效:
SELECT E.empid, E.fname, E.lname, EH.hoursworked, DH.deductioncode
FROM Employee E
INNER JOIN EarnHistory EH ON E.empid = EH.empid
INNER JOIN DeductionHistory DH ON E.empid = DH.empid
LEFT JOIN DeductionHistory DH2 ON E.empid = DH2.empid
AND DH2.deductioncode IN (7001,7002,7003,7004,7005,7006)
WHERE DHD2.empid IS NULL
这取决于您的扣减代码数据类型。您可以使用BETWEEN,<>或添加撇号(如果varchar。
)您也可以使用NOT EXISTS
:
SELECT E.empid, E.fname, E.lname, EH.hoursworked, DH.deductioncode
FROM Employee E
INNER JOIN EarnHistory EH ON E.empid = EH.empid
INNER JOIN DeductionHistory DH ON E.empid = DH.empid
WHERE NOT EXISTS (SELECT *
FROM DeductionHistory
WHERE deductioncode IN (7001,7002,7003,7004,7005,7006)
AND E.empid = DeductionHistory.empid)
祝你好运。
答案 1 :(得分:0)
select Employee.empid, Employee.fname , Employee.lname,EarnHistory.hoursworked, DeductionHistory.deductioncode from Employee
join EarnHistory using('empid')
join DeductionHistory using('empid')
LEFT JOIN DeductionHistory dh ON Empployee.empid = dh.empid
AND DH.deductioncode IN (7001,.......,7006)
WHERE DHD.empid IS NULL
答案 2 :(得分:0)
SELECT E.empid, E.fname, E.lname, EH.hoursworked, DH.deductioncode
FROM Employee E, EarnHistory EN, DeductionHistory DH
WHERE E.empid = EH.empid
AND E.empid = DH.empid
AND E.empid <> (SELECT empid
FROM DedutionHistory
GROUP BY empid
HAVING deductioncode >= 7001
AND deductioncode <= 7006)