1

谁能告诉我如何从这个声明中获取记录

  • 选择最近 x 个月内不是当月员工的随机员工

表员工
ID
EmployeeName

表 EmployeeOfTheMonth
ID
EmployeeID
MonthStartedDate
MonthEndedDate

非常感谢

4

2 回答 2

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 服务器上,这应该是正确的。

于 2009-05-07T02:33:24.280 回答
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()

于 2009-05-08T08:20:50.193 回答