我有一个查询来为给定的员工选择最近的加薪:
SELECT TOP 1 Raises.EmployeeID, Raises.RaiseAmt, Raises.Date, Raises.SalaryPrior
FROM Raises
WHERE (((Raises.EmployeeID)=[Enter Employee ID: ]))
ORDER BY Raises.Date DESC;
对于已经收到多次加薪的员工,如何显示第一次和第二次加注之间超过的时间?
答案 0 :(得分:0)
看起来你正在使用SQL Server。您可以使用公用表表达式简化此操作:
WITH recent_raises AS (
SELECT TOP 2 Raises.EmployeeID, Raises.RaiseAmt, Raises.Date, Raises.SalaryPrior
FROM Raises
WHERE (((Raises.EmployeeID)=[Enter Employee ID: ]))
ORDER BY Raises.Date DESC;
),
SELECT DATEDIFF(day, r1.Date, r2.Date) AS elapsed_days
FROM recent_raises r1
JOIN recent_raises r2
ON r1.Date < r2.Date
答案 1 :(得分:0)
尝试此查询(仅当当前员工至少有两次加薪时才有效):
SELECT y.*, DATEDIFF(DAY, y.[2], y.[2]) AS DaysBetweenTwoRaises
FROM
(
SELECT TOP(2) Raises.Date, ROW_NUMBER() OVER(ORDER BY Raises.Date DESC) RowNum
FROM Raises
WHERE Raises.EmployeeID=[Enter Employee ID: ]
ORDER BY Raises.Date DESC
) x
PIVOT ( MAX(x.Date) FOR x.RowNum IN ([1], [2]) ) y;