包括离职员工 6 个月内的日期,同时仍显示活跃员工 SQL 服务器

Include dates within 6 months of terminated employees while still displaying active employees SQL Server

我试图在我的查询中包括所有离职日期在 6 个月前的员工,同时还包括在职员工。我已经包含了以下内容,但这样做会过滤掉活跃的员工。我怎样才能做到这一点?

JOIN Employees1 e
    ON e.ClientID = c.ClientID
JOIN Employees2 d
    ON d.ClientID = e.ClientID
    AND d.EmployeeUID = e.EmployeeUID
    AND TerminationDate >= Dateadd(Month, Datediff(Month, 0, DATEADD(m, -6,current_timestamp)), 0)

此外,有没有一种方法可以返回到当前日期之前 6 个月的确切日期,而不是包括 6 个月之前的整个月?

与其在 joins 中包含逻辑,不如尝试以下内容:

SELECT
    e.*
FROM
   Employees1 e
WHERE
    e.TerminateDate IS NULL
UNION ALL
SELECT
    e1.*
FROM
   Employees1 e1
WHERE
    e1.TerminateDate >= DATEADD(m, -6, GetDate())


甚至更好,请参阅 Gordon Linoff 的回答。

如果只想要在职或满足条件的员工,则在FROM子句中使用OR

select e.*
from employees e
where e.TerminationDate >= DATEADD(month, -6, current_timestamp) OR
      e.TerminationDate IS NULL;