我如何找出在某个时间间隔内未处于活动状态的用户

问题描述 投票:0回答:3

下面的脚本演示了我如何找出在一段时间内处于活动状态的用户。如何确定在此时间间隔内哪些用户未处于活动状态?

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'

提前感谢

sql sql-server tsql
3个回答
0
投票

使用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' 
                 );

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


0
投票

考虑将WHERE子句更改为类似内容:

...
WHERE a.CreationDate < '2019-01-01' and a.CreationDate > '2019-12-31'

[快乐黑客:)

© www.soinside.com 2019 - 2024. All rights reserved.