我正在尝试做这样的事情:
SELECT UserID from Users as user
INNER JOIN UserStatus as userStatus on u.UserID = us.UserID
WHERE
--user.IsActive = 1
--if user.IsActive = 0
--userStatus.DeactivatedDate > @StartDate
--userStatus.DeactivatedDate < @EndDate
所以一般来说我想要一个查询来获取所有结果,其值与where语句匹配,而不必忽略所有那些没有的结果。有点像维恩图,圆圈的左侧和中间填充。对于我已经显示的示例,我希望所有当前处于活动状态的用户和不活动但在给定时间内处于活动状态的用户frame(例如,如果我想要在一个月内的任何时间点活动的用户总数)。
我尝试过像
这样的事情SELECT UserID from Users as user
INNER JOIN UserStatus as userStatus on u.UserID = us.UserID
WHERE
Case user.IsActive = 1
但这显然是不正确的语法。我还需要将其转换为LINQ,因此这方面的资源也是有益的。
答案 0 :(得分:4)
我相信这会给你你想要的结果:
SELECT UserID
FROM Users U
JOIN UserStatus US
ON U.UserID = US.UserID
WHERE
U.IsActive = 1
OR (
U.IsActive = 0
AND US.DeactivatedDate > @StartDate
AND US.DeactivatedDate < @EndDate
)
答案 1 :(得分:3)
SELECT UserID from Users as user
INNER JOIN UserStatus as userStatus on u.UserID = us.UserID
WHERE
user.IsActive = 1 OR us.DeactvatedDate BETWEEN '2012-08-13' AND '2013-08-13'
如果你有关于用户被解除激活的日期的任何条目,这将完成这项工作。
在LINQ中
var UID= from u in Users
from s in UserStatus
where u.UserID == s.UserID || (s.DeactivatedDate >= @StartDate &&
s.DeactivatedDate <= @endDate)
select u.userID