Find subscribers who did not resubscribe within threshold of old subscription

别等时光非礼了梦想. 提交于 2019-12-11 04:29:58

问题


I have a table Subscriptions in 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/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

I want to see user_ids who do not have a subscription that starts within 2 days of when their March subscription ended. For users with a subscription that ends in March.

Given the table above, the results would be:

user_id
-------
296
126
922
111

How would I go about putting that query together for March?


回答1:


Not exists should do what you want:

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


来源:https://stackoverflow.com/questions/55618271/find-subscribers-who-did-not-resubscribe-within-threshold-of-old-subscription

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!