我将这些表格包含以下列:
我想写一个查询来查找已经获得最大休假天数的员工:
select emp.*, L.Startdate - L.Enddate
from leave25 l, employee24 emp
where emp.employeeid = l.employeeid
and l.startdate - l.enddate = (select max(startdate - enddate) from leave25)
答案 0 :(得分:1)
你可以使用以下嵌套SQL和DENSE_RANK()函数(包括tie(相同的值)):
SELECT EMPLOYEEID, FIRSTNAME, LASTNAME, LEAVE_DAY
FROM
(
SELECT L.EMPLOYEEID, FIRSTNAME, LASTNAME,
(ENDDATE - STARTDATE + 1) LEAVE_DAY,
DENSE_RANK() OVER (ORDER BY (ENDDATE - STARTDATE + 1) DESC) DR
FROM EMPLOYEE24 E JOIN LEAVE25 L ON ( E.employeeid = L.employeeid )
GROUP BY L.EMPLOYEEID, FIRSTNAME, LASTNAME, ENDDATE - STARTDATE + 1
)
WHERE DR = 1
ORDER BY 1;
修改 (根据您的评论" 查询过去1个月内获得最多离职数量的员工&# 34;):
SELECT EMPLOYEEID, FIRSTNAME, LASTNAME,
LEAVE_DAY
FROM
(
SELECT EMPLOYEEID, FIRSTNAME, LASTNAME,
LEAVE_DAY, DENSE_RANK() OVER (ORDER BY LEAVE_DAY DESC) DR
FROM
(
SELECT L.EMPLOYEEID, FIRSTNAME, LASTNAME,
DECODE(SIGN(ENDDATE - ADD_MONTHS(TRUNC(SYSDATE),-1)+1),-1,0,
ENDDATE - ADD_MONTHS(TRUNC(SYSDATE),-1)+1) -
DECODE(SIGN(STARTDATE - ADD_MONTHS(TRUNC(SYSDATE),-1)+1),1,
(STARTDATE - ADD_MONTHS(TRUNC(SYSDATE),-1)),0)
as LEAVE_DAY
FROM EMPLOYEE24 E JOIN LEAVE25 L ON ( E.employeeid = L.employeeid )
)
)
WHERE DR = 1
ORDER BY 1;
答案 1 :(得分:0)
试试这个:
Select Top 1 e.EMPLOYEEID,e.Name,SUM(DATEDIFF(d,StartDate,EndDate)+1) as LeaveTaken
from Leave25 l
INNER JOIN Employee24 e on l.EMPLOYEEID=e.EMPLOYEEID
Group By e.EMPLOYEEID,e.Name
Order by LeaveTaken desc