有没有人能告诉我如何从这个陈述中获取记录
表员工
ID
员工姓名
表EmployeeOfTheMonth
ID
雇员
MonthStartedDate
MonthEndedDate
非常感谢
答案 0 :(得分:2)
select top 1 id from employee as emp
where not exist(
select top 1 *
from employeeofthemonth as em
where em.id = emp.id and dateadd(m, -6, getdate()) < monthendeddt )
order by newid()
......或者接近那个的东西。我没有运行sql,但是在mssql服务器上,这应该是正确的。
答案 1 :(得分:0)
修改上一个答案,我更喜欢使用IN语句
DECLARE @xMonth INT
SET @xMonth = 3
SELECT TOP 1 ID
FROM Employee
WHERE ID NOT IN (SELECT EmployeeID
FROM EmployeeOfTheMonth
WHERE DATEADD(m, -@xMonth, GETDATE()) < MonthEndedDate)
ORDER BY NEWID()