0

我在这里的第一篇文章,希望能有所帮助。我需要创建一份报告,显示从未支付退休金的员工。我需要从 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 where 员工从未支付到deduction code 7001到7006。

我尝试使用 NOT EXISTS 失败

4

3 回答 3

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
于 2013-02-02T05:15:56.837 回答
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

这取决于您的 deductioncode 数据类型。您可以使用 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)

祝你好运。

于 2013-02-02T05:08:43.683 回答
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)
于 2013-02-02T10:12:22.650 回答