如何检查给定日期是否至少包括为用户列出的每个文档组?
How do I check if a given date includes at least each of the document group listed for user?
我想开发一个 SQL 查询来检查给定日期是否至少在每个文档组中。
下面的table
DocID UserID StartDAte EndDAte OfficialName
1 1 10/1/18 10/3/18 A
2 1 10/5/18 10/10/18 A
3 1 10/1/18 10/9/18 B
4 1 10/1/18 10/9/18 C
5 1 10/1/18 10/5/18 D
6 1 10/7/18 10/20/18 D
有4个文件组,分别是A,B,C,D。需要检查给定日期是否至少在每个组中的每个文档中。
eg date : 10/2/18 is in first record of A,B,C, and first record of D. So it is passed.
eg date : 10/4/18 is not in either of documents in A hence failed.
eg date : 10/8/18 is second document in A,B,C, and second document in D hence passed.
eg date : 10/6/18 is in A but not in D hence failed.
因为我必须为给定的用户和日期编写此代码,所以我必须对 "OfficialName" 使用 "IN" 子句,但是我如何添加 "OR" 来检查日期是否在任何给定用户的所有文档的每个 "OfficialName" 组中的文件数?
感谢任何帮助。
需要补充一些不清楚的地方。正式名称中的文件数量不固定。它可以是一个或多个。
聚合并获取不同的组数。如果你得到 4,你有一个匹配,否则你没有。
SELECT count(DISTINCT t.officialname)
FROM elbat t
WHERE t.userid = <given user>
AND t.startdate <= <given date>
AND t.enddate >= <given date>;
当且仅当没有匹配项时,您还可以添加 HAVING count(DISTINCT t.officialname) = 4
以获得空集。
你可以使用这个:
SELECT count(DISTINCT t.officialname)
FROM elbat t
WHERE @date between t.startDate AND t.enddate and
t.userid = @userId;
我想你想要:
select (case when count(distinct t.officialname) = 4 then 'passed' else 'failed' end) as flag_4groups
from t
where @date <= t.startdate and
@date >= t.enddate and
t.user_id = @user;
如果您希望对所有用户(但给定日期)执行此操作:
select t.user_id,
(case when count(distinct t.officialname) = 4 then 'passed' else 'failed' end) as flag_4groups
from t
where @date <= t.startdate and
@date >= t.enddate
group by t.user_id
我想开发一个 SQL 查询来检查给定日期是否至少在每个文档组中。 下面的table
DocID UserID StartDAte EndDAte OfficialName
1 1 10/1/18 10/3/18 A
2 1 10/5/18 10/10/18 A
3 1 10/1/18 10/9/18 B
4 1 10/1/18 10/9/18 C
5 1 10/1/18 10/5/18 D
6 1 10/7/18 10/20/18 D
有4个文件组,分别是A,B,C,D。需要检查给定日期是否至少在每个组中的每个文档中。
eg date : 10/2/18 is in first record of A,B,C, and first record of D. So it is passed.
eg date : 10/4/18 is not in either of documents in A hence failed.
eg date : 10/8/18 is second document in A,B,C, and second document in D hence passed.
eg date : 10/6/18 is in A but not in D hence failed.
因为我必须为给定的用户和日期编写此代码,所以我必须对 "OfficialName" 使用 "IN" 子句,但是我如何添加 "OR" 来检查日期是否在任何给定用户的所有文档的每个 "OfficialName" 组中的文件数?
感谢任何帮助。 需要补充一些不清楚的地方。正式名称中的文件数量不固定。它可以是一个或多个。
聚合并获取不同的组数。如果你得到 4,你有一个匹配,否则你没有。
SELECT count(DISTINCT t.officialname)
FROM elbat t
WHERE t.userid = <given user>
AND t.startdate <= <given date>
AND t.enddate >= <given date>;
当且仅当没有匹配项时,您还可以添加 HAVING count(DISTINCT t.officialname) = 4
以获得空集。
你可以使用这个:
SELECT count(DISTINCT t.officialname)
FROM elbat t
WHERE @date between t.startDate AND t.enddate and
t.userid = @userId;
我想你想要:
select (case when count(distinct t.officialname) = 4 then 'passed' else 'failed' end) as flag_4groups
from t
where @date <= t.startdate and
@date >= t.enddate and
t.user_id = @user;
如果您希望对所有用户(但给定日期)执行此操作:
select t.user_id,
(case when count(distinct t.officialname) = 4 then 'passed' else 'failed' end) as flag_4groups
from t
where @date <= t.startdate and
@date >= t.enddate
group by t.user_id