【问题标题】:the date in between两者之间的日期
【发布时间】:2018-02-20 15:49:11
【问题描述】:

表格测试:

    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_date

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

那么resolve会是这样的。

  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 天内。

【问题讨论】:

  • 用您正在使用的数据库标记您的问题。
  • 如果还有一个日期“2001-02-01”,即在“02-27”的 28 天内而不是“03-23”的 28 天内,该怎么办?因此,请详细解释一下要求(作为对您问题的修改)
  • 您可以阅读以便更好地接收您的问题minimal reproducible example

标签: sql database sql-server-2008


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多