SQL 查询帮助:随机选择当月员工

发布于 2024-07-19 05:57:37 字数 211 浏览 2 评论 0原文

任何人都可以告诉我如何从此语句中获取记录

  • 选择随机员工,该员工不是过去 x 个月内该月的员工

表员工
身份证号
EmployeeName

表 EmployeeOfTheMonth
身份证号
员工ID
月份开始日期
月结束日期

非常感谢

Could any one show me how to get record from this statement

  • Select random employee which is not an employee of the month in the last x months

Table Employee
ID
EmployeeName

Table EmployeeOfTheMonth
ID
EmployeeID
MonthStartedDate
MonthEndedDate

Thank you very much

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云淡风轻 2024-07-26 05:57:38

修改上一个答案,我更喜欢使用 IN 语句


DECLARE @xMonth INT
SET @xMonth = 3

SELECT TOP 1 ID 来自员工 WHERE ID NOT IN(选择 EmployeeID 来自本月最佳员工 其中 DATEADD(m, -@xMonth, GETDATE()) < 月结束日期) 按 NEWID() 排序

Modification of last answer, i prefer to use IN statment


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()

等风来 2024-07-26 05:57:37
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服务器上,这应该是正确的。

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()

... or something close to that. i didn't run the sql, but on mssql server, this should be about right.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文