Sql 查找未在旧订阅阈值内重新订阅的订阅服务器
我在PostgreSQL 10.5中有一个表订阅:Sql 查找未在旧订阅阈值内重新订阅的订阅服务器,sql,postgresql,date-range,Sql,Postgresql,Date Range,我在PostgreSQL 10.5中有一个表订阅: id user_id starts_at ends_at -------------------------------- 1 233 02/04/19 03/03/19 2 233 03/04/19 04/03/19 3 296 02/09/19 03/08/19 4 126 02/01/19 02/28/19 5 126 03/01/19 03/
id user_id starts_at ends_at
--------------------------------
1 233 02/04/19 03/03/19
2 233 03/04/19 04/03/19
3 296 02/09/19 03/08/19
4 126 02/01/19 02/28/19
5 126 03/01/19 03/31/19
6 922 02/22/19 03/22/19
7 111 01/22/19 02/21/19
8 111 02/22/19 03/21/19
我想查看那些没有在3月份订阅结束后2天内开始订阅的用户ID。适用于订阅将于3月结束的用户
根据上表,结果如下:
user_id
-------
296
126
922
111
我将如何在3月份完成该查询?不存在应该做您想做的事情:
select t.*
from t
where ends_at >= '2019-03-01' and ends_at < '2019-04-01' and
not exists (select 1
from t t2
where t2.user_id = t.user_id and
t2.starts_at >= t.ends_at and
t2.starts_at <= t.ends_at + interval '2 day'
);
这非常有效,非常值得赞赏。我忘了我可以将订阅开始/结束日期和用户id传递到“不存在”的子选项中。谢谢