Sql 中间的日期
表测试:Sql 中间的日期,sql,database,sql-server-2008,Sql,Database,Sql Server 2008,表测试: ID code vac_date 419199 1 2004-06-24 419199 2 2001-10-04 419199 1 2005-02-09 419199 2 2001-03-23 419199 2 2001-03-22 419199 1 2001-03-22 419199 1 2001-02-27 419199 1 2001-04-04 我试
ID code vac_date
419199 1 2004-06-24
419199 2 2001-10-04
419199 1 2005-02-09
419199 2 2001-03-23
419199 2 2001-03-22
419199 1 2001-03-22
419199 1 2001-02-27
419199 1 2001-04-04
我试着只包含彼此28天内的vac_日期
select ID, code, vac_date
from test as a
where a.vac_date = a.vac_date - 28
and a.vac_date = a.vac_date + 28
那么这个决心是这样的
419199 2 2001-03-23
419199 2 2001-03-22
419199 1 2001-03-22
419199 1 2001-02-27
419199 1 2001-04-04
只要日期在28天之内。24天可以是任意一个方向。日期/时间函数也非常特定于数据库。下面介绍了您可以做的事情:
select t.*
from t
where exists (select 1
from t t2
where t2.id = t.id and
t2.code = t.code and
t2.vac_date >= t.vac_date - interval '24 day' and
t2.vac_date <= t.vac_date - interval '24 day'
);
用你正在使用的数据库标记你的问题。如果还有日期2001-02-01,即02-27的28天内,而不是03-23的28天内,该怎么办?因此,请解释更多关于要求的内容,作为对您的问题的编辑,您可以阅读,以便更好地收到您的问题