1

我有 3 个表如何使用 manageriD 在特定日期范围内获取所有员工的缺席

1)日期查找表(所有月份的查找表)*/ 示例

╔════════════╗
║    DATE    ║
╠════════════╣
║ 2013-01-01 ║
║ 2013-01-02 ║
║ 2013-01-03 ║
╚════════════╝

第二个员工表,其中我有列,例如(所有具有经理 ID 的员工的员工表)

╔════════════╦══════════╦════════╦══════════╦═══════╦═══════════╗
║ EMPLOYEEID ║ BRANCHID ║ DEPTID ║ DESIGNID ║ NAME  ║ MANAGERID ║
╠════════════╬══════════╬════════╬══════════╬═══════╬═══════════╣
║          1 ║        1 ║     10 ║        5 ║ A     ║        15 ║
║          2 ║        2 ║     10 ║        5 ║ AB    ║        18 ║
║          3 ║        1 ║     10 ║        5 ║ ABC   ║        15 ║
║          5 ║        1 ║     10 ║        5 ║ ABCD  ║        15 ║
║          9 ║        2 ║     10 ║        5 ║ AEFG  ║        16 ║
║         12 ║        1 ║     10 ║        5 ║ AHIJ  ║        15 ║
║          8 ║        1 ║     10 ║        5 ║ AKLMN ║        18 ║
╚════════════╩══════════╩════════╩══════════╩═══════╩═══════════╝ 

第三个表是登录和退出表(所有员工的登录和注销)

╔════════════╦══════════╦═════════════════════╦═════════════╗
║ EMPLOYEEID ║ BRANCHID ║    CHECKLOGSDATE    ║ CHECKINTYPE ║
╠════════════╬══════════╬═════════════════════╬═════════════╣
║          1 ║        1 ║ 2013-01-01 09:12:15 ║ in          ║
║          1 ║        2 ║ 2013-01-01 19:24:15 ║ out         ║
║          2 ║        1 ║ 2013-01-01 10:20:15 ║ in          ║
║          2 ║        1 ║ 2013-01-01 18:24:15 ║ out         ║
║          3 ║        2 ║ 2013-01-01 09:12:15 ║ in          ║
║          3 ║        1 ║ 2013-01-01 16:24:15 ║ out         ║
║          4 ║        1 ║ 2013-01-01 09:32:15 ║ in          ║
╚════════════╩══════════╩═════════════════════╩═════════════╝

SQLFiddle 演示

4

2 回答 2

1

从您的问题中不清楚有什么规则可以告诉员工在任何一天都缺勤。如果日志中没有任何记录(输入或输出),那么您可以尝试

更新

SELECT d.date, e.employeeid, e.name
  FROM lookup d CROSS JOIN employee e LEFT JOIN log l 
    ON d.date = DATE(l.Checklogsdate)
   AND e.employeeid = l.employeeid   
 WHERE e.managerid = 15  
   AND d.date BETWEEN '2013-01-01' AND '2013-01-02'
   AND l.employeeid IS NULL
 GROUP BY d.date, e.employeeid, e.name 

这是SQLFiddle演示。

原来的

SELECT e1.date, e1.employeeid, e1.name
  FROM
(
 SELECT d.date, e.employeeid, e.name
   FROM lookup d CROSS JOIN employee e
   WHERE managerid = 15  
     AND d.date BETWEEN '2013-01-01' AND '2013-01-02'
) e1 LEFT JOIN
(
 SELECT l.employeeid, DATE(MIN(l.Checklogsdate)) date
   FROM log l JOIN employee e 
     ON l.employeeid = e.employeeid
    AND e.managerid = 15  
  GROUP BY l.employeeid, DATE(l.Checklogsdate)
) l1
    ON e1.date = l1.date
   AND e1.employeeid = l1.employeeid 
 WHERE l1.employeeid IS NULL
 ORDER BY e1.date, e1.employeeid

样本输出:

|             DATE | EMPLOYEEID | NAME |
----------------------------------------
| January, 01 2013 |          5 | ABCD |
| January, 01 2013 |         12 | AHIJ |
| January, 02 2013 |          1 |    A |
| January, 02 2013 |          3 |  ABC |
| January, 02 2013 |          5 | ABCD |
| January, 02 2013 |         12 | AHIJ |

这是SQLFiddle演示。

于 2013-06-03T08:55:45.117 回答
0
SELECT e.Name
FROM Employee e
WHERE e.ManagerId = 15
AND e.Name NOT IN
(
    SELECT DISTINCT e.Name
    FROM Log l
    INNER JOIN Employee e ON l.EmployeeId = e.EmployeeId
    WHERE e.ManagerId = 15
)

这里15将替换为您想要的号码

SQL 小提琴演示

于 2013-06-03T08:39:34.290 回答