我需要一个SQL查询来检查一个人在一年中连续两个星期是否活跃。
例如,
Table1:
Name | Activity | Date
Name1|Basketball| 08-08-2014
Name2|Volleyball| 08-09-2014
Name3|None | 08-10-2014
Name1|Tennis | 08-14-2014
我要检索Name1,因为该人在一年中连续两个星期都处于活动状态。
到目前为止,这是我的查询:
SELECT DISTINCT Name
FROM Table1
Where YEAR(Date) = 2014 AND
Activity NOT 'None' AND
这是我需要连续两周检查活动的逻辑的地方。一周可以说是7到14天后。我正在使用MYSQL。
您可以使用exists
子查询来执行逻辑:
select t.*
from table1 t
where exists (select 1
from table1 t2
where t2.name = t.name and
t2.date between t.date + 7 and t.date + 14
);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句