查找未重新订阅的用户

时间:2019-04-09 02:27:29

标签: sql postgresql date-range

我在PostgreSQL 10.5中有一张表Subscriptions:

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月重新订阅的用户ID的列表。鉴于以上数据,它应该显示:

user_id
-------
296
922

我将如何进行计算。我尝试了一些查询,但它们不起作用,也不值得发布

3 个答案:

答案 0 :(得分:1)

您可以利用不存在,也不会吸引那些开始日期为三月的客户。

with cte as 
(
select 1  as ID,   233 as User_Id, '02/04/2019' as Startsat   , '03/03/2019' ends_at union all 
select 2  as ID,   233 as User_Id, '03/04/2019' as Startsat   , '04/03/2019' ends_at union all 
select 3  as ID,   296 as User_Id, '02/09/2019' as Startsat   , '03/08/2019' ends_at union all 
select 4  as ID,   126 as User_Id, '02/01/2019' as Startsat   , '02/28/2019' ends_at union all 
select 5  as ID,   126 as User_Id, '03/01/2019' as Startsat   , '03/31/2019' ends_at union all 
select 6  as ID,   922 as User_Id, '02/22/2019' as Startsat   , '03/22/2019' ends_at)

select *  from cte  c 
where  not exists
(select 1 from cte c1 where c.User_Id = c1.User_Id and date_part('Month',to_date(c1.Startsat,'MM/DD/YYYY'))= '3' )

输出:

id  user_id startsat    ends_at
3   296 02/09/2019  03/08/2019
6   922 02/22/2019  03/22/2019

以下是小提琴链接:

https://dbfiddle.uk/?rdbms=postgres_10&fiddle=84e24cd517fa0810bef011d6fb1b2be2

答案 1 :(得分:1)

大概是您想要一个特定的三月,而不是任何一年的三月。所以:

select s.userId
from subscriptions s
group by s.userId
having count(*) filter (where startsAt >= '2019-03-01' and startsAt < '2019-04-01') = 0;

您也可以使用not exists。如果您有用户列表,则效果更好:

select u.*
from users u
where not exists (select 1
                  from subscriptions s
                  where s.userid = u.userid and
                        s.startsAt >= '2019-03-01' and
                        s.startsAt < '2019-04-01'
                 );

除了users,您还可以使用:

select distinct s.userId
from subscriptions
where . . .

答案 2 :(得分:0)

除了其他答案,这里还有其他几个选择:

选项1

您可以创建2个CTE,每个月1个(假设您查看的是特定月份,而不仅仅是一般的2月/ 3月)。请注意,这使用range数据类型来过滤日期。

WITH 
    -- sample data
    Subscriptions("id", user_id, starts_at, ends_at) AS
    (
        VALUES
        (1,   233,      DATE'02/04/19',   DATE'03/03/19'),
        (2,   233,      DATE'03/04/19',   DATE'04/03/19'),
        (3,   296,      DATE'02/09/19',   DATE'03/08/19'),
        (4,   126,      DATE'02/01/19',   DATE'02/28/19'),
        (5,   126,      DATE'03/01/19',   DATE'03/31/19'),
        (6,   922,      DATE'02/22/19',   DATE'03/22/19')
    ),
    -- separate CTEs for February and March data
    -- using range type for easy filter.
    FebruarySubscriptions AS
    (
        SELECT * FROM Subscriptions 
        WHERE daterange('2019-02-01', '2019-03-01') @> starts_at
    ),
    MarchSubscriptions AS
    (
        SELECT * FROM Subscriptions 
        WHERE daterange('2019-03-01', '2019-04-01') @> starts_at
    )
SELECT * 
FROM FebruarySubscriptions
    LEFT JOIN MarchSubscriptions ON
        MarchSubscriptions.user_id = FebruarySubscriptions.user_id
WHERE MarchSubscriptions."id" IS NULL

选项2

使用LEAD窗口功能可以确定哪些用户没有重新订阅。此选项的好处是它更具扩展性。

WITH 
    Subscriptions("id", user_id, starts_at, ends_at) AS
    (
        VALUES
        (1,   233,      DATE'02/04/19',   DATE'03/03/19'),
        (2,   233,      DATE'03/04/19',   DATE'04/03/19'),
        (3,   296,      DATE'02/09/19',   DATE'03/08/19'),
        (4,   126,      DATE'02/01/19',   DATE'02/28/19'),
        (5,   126,      DATE'03/01/19',   DATE'03/31/19'),
        (6,   922,      DATE'02/22/19',   DATE'03/22/19')
    ),
    Resubscriptions(user_id, current_subscription, next_subscription) AS
    (
        SELECT 
            user_id, 
            starts_at, 
            LEAD(starts_at) OVER
            (
                PARTITION BY user_id
                ORDER BY starts_at ASC
            )
        FROM Subscriptions
    )
SELECT * 
FROM Resubscriptions
WHERE 
    daterange('2019-02-01', '2019-03-01') @> current_subscription
    AND next_subscription IS NULL