sql 参考日期前后的持续时间

sql duration around the reference date

table 客户中有一群客户进行了一些活动,每个客户都执行了特定的 activity 并且它已被标记为事件发生并且日期已 selected 作为参考日期。

现在的任务是查找参考日期前后客户的记录。例如。 select 每个客户在事件发生前 10 天或事件发生后 20 天或事件发生前 5 天和事件后 5 天的活动。

table 看起来像:

Customer ID| activities | date
 1          | a1         | date1
 1          | a2         | date1
 1          | a3         | date2
 1          | a1         | date2
 .
 1          | a-sp       | date22 ---> a-sp is an event occurred and the date is Ref. Date
 .
 1          | a1000      | date30

许多其他客户也是如此

社区有什么建议吗?

你可以这样做:

select t.*, e.event_date
from t join
     (select t.customerid, t.date as event_date
      from t
      where t.activity = 'a-sp'
     ) e
     on t.date >= add_days(e.event_date, -5) and
        t.date <= add_days(e.evant_date, 5);