下面的脚本演示了如何确定在一段时间内哪些用户处于活动状态。如何确定在此时间间隔内哪些用户未处于活动状态?
USE Database
SELECT
a.CreationDate
, c.UserName AS Name
, b.Fullname as DepartmentName
, c.FirstName as Forename
, c.LastName as Surname
FROM [dbo].[CaseTable] as a
INNER JOIN [dbo].[tbl_Departments] as b ON a.DepartmentID =b.DepartmentID
INNER JOIN [dbo].[tbl_Users] as c ON c.UserID =a.UserID
WHERE (a.CreationDate) between '2019-01-01' and '2019-12-31'
预先感谢
答案 0 :(得分:1)
使用not exists
:
SELECT u.*
FROM [dbo].[tbl_Users] u
WHERE NOT EXISTS (SELECT 1
FROM [dbo].[CaseTable] ct
WHERE c.tUserID = u.UserID AND ct.CreationDate between '2019-01-01' and '2019-12-31'
);
答案 1 :(得分:0)
请考虑将您的WHERE
子句更改为以下内容:
...
WHERE a.CreationDate < '2019-01-01'
答案 2 :(得分:0)
NOT BETWEEN
是适合您的解决方案吗?
¨
SELECT a.CreationDate
,c.UserName AS Name
,b.Fullname as DepartmentName
,c.FirstName as Forename
,c.LastName as Surname
FROM [dbo].[CaseTable] as a
INNER JOIN [dbo].[tbl_Departments] as b ON a.DepartmentID =b.DepartmentID
INNER JOIN [dbo].[tbl_Users] as c ON c.UserID =a.UserID
WHERE (a.CreationDate) not between '2019-01-01' and '2019-12-31'
例如,这里有一个小的DEMO