找到过去1年没有休一次假的员工

时间:2018-05-31 04:25:30

标签: sql oracle oracle11g

我将这些表格包含以下列:

Employee24( EMPLOYEEID, FIRSTNAME, LASTNAME, GENDER );

Leave25( EMPLOYEEID,LEAVEID, LEAVETYPE, STARTDATE, ENDDATE);

我想写一个查询来查找过去1年没有休一次假的员工

SELECT FIRSTNAME, LASTNAME FROM EMPLOYEE24 E INNER JOIN ON LEAVE25 L WHERE E.EMPLOYEEID = L.EMPLOYEEID AND L.EMPLOYEEID = NULL AND STARTDATE > ADD_MONTHS(SYSDATE, -12)

4 个答案:

答案 0 :(得分:2)

在您的案例中使用 NOT IN 是合适的,如下所示:

SELECT FIRSTNAME, LASTNAME
  FROM EMPLOYEE24 E
  WHERE E.EMPLOYEEID NOT IN ( SELECT L2.EMPLOYEEID 
                                FROM LEAVE25 L2 
                               WHERE STARTDATE >= ADD_MONTHS(SYSDATE, -12) );

SQL Fiddle Demo

答案 1 :(得分:1)

您不一定需要使用连接和子查询。您只需使用以下命令即可:

SELECT E.*
FROM Employee24 E
WHERE NOT EXISTS (SELECT NULL FROM Leave25 L 
                  WHERE E.EMPLOYEEID=L.EMPLOYEEID 
                  AND (STARTDATE>=SYSDATE-INTERVAL '12' MONTH
                  OR ENDDATE>=SYSDATE-INTERVAL '12' MONTH));

查看Demo on SQL Fiddle

答案 2 :(得分:0)

您可以使用以下查询查找过去一年没有休假的员工。

 SELECT E.EMPLOYEEID, E.FIRSTNAME, E.LASTNAME, MAX(L.STARTDATE), MAX(L.ENDDATE)
FROM EMPLOYEE24 E LEFT JOIN
     LEAVE25 L
     ON E.EMPLOYEEID = L.EMPLOYEEID 
GROUP BY E.EMPLOYEEID, E.FIRSTNAME, E.LASTNAME
HAVING COALESCE(MAX(L.STARTDATE),date'1900-01-01') < ADD_MONTHS(SYSDATE,-12) 

SQLFiddle Reference

答案 3 :(得分:-1)

SELECT * FROM employee24 e 
WHERE NOT EXISTS (
    SELECT 1 FROM leaves25 l
    WHERE startdate > 'yearstartdate'
    AND enddate < 'yearenddate'
    WHERE l.employeeid = e.employeeid
)