【问题标题】:SQL Logic for calculating the unique members count for rolling 3 months by each month用于计算每个月滚动 3 个月的唯一成员数的 SQL 逻辑
【发布时间】:2020-03-04 07:56:44
【问题描述】:

我正在处理一个请求,以计算以下日期桶的表中唯一注册成员的数量(基于registration_date_utc - 时间戳),

1 月 1 日至 3 月 31 日:唯一注册会员数

2 月 1 日至 4 月 30 日:唯一注册会员数

3 月 1 日至 5 月 31 日:唯一注册会员计数

4 月 1 日至 6 月 30 日:唯一注册会员数

5 月 1 日至 7 月 31 日:唯一注册会员计数

6 月 1 日至 8 月 31 日:唯一注册会员数

7 月 1 日至 9 月 30 日:唯一注册会员数

8 月 1 日至 10 月 31 日:唯一注册成员计数

9 月 1 日至 11 月 30 日:唯一注册成员计数

10 月 1 日至 12 月 31 日:唯一注册成员计数

下面是我的 Snowflake 查询,它有效,但没有返回任何结果,

select
DATE_TRUNC('month',t.registration_date_utc) as "MONTH_START",
count(distinct t.MEMBER_ID)
FROM
(
select
member_id,
registration_date_utc
from  table a
) as t
WHERE TO_DATE(t.registration_date_utc) BETWEEN DATEADD(month,'3',t.registration_date_utc) AND t.registration_date_utc
group by 1
order by 1;

【问题讨论】:

    标签: sql date timestamp snowflake-cloud-data-platform


    【解决方案1】:

    在 START_DATE 和 END_DATE 之间 你的日期条件永远是假的

    select
    DATE_TRUNC('month',t.registration_date_utc) as "MONTH_START",
    count(distinct t.MEMBER_ID)
    FROM
    (
    select
    member_id,
    registration_date_utc
    from  table a
    ) as t
    --WHERE TO_DATE(t.registration_date_utc) BETWEEN DATEADD(month,'3',t.registration_date_utc) AND t.registration_date_utc
    WHERE TO_DATE(t.registration_date_utc) BETWEEN t.registration_date_utc AND DATEADD(month,'3',t.registration_date_utc) 
    
    group by 1
    order by 1;
    

    【讨论】:

      【解决方案2】:

      您想要统计三个月的唯一身份访问者。一种方法是展开表进行计数:

      SELECT DATE_TRUNC('month', t.registration_date_utc) + v.offset * INTERVAL '1 month') as MONTH_START,
             COUNT(DISTINCT t.MEMBER_ID)
      FROM t CROSS JOIN
           (VALUES (0), (1), (2)) v(offset)
      GROUP BY 1
      ORDER BY 1;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-05-10
        • 1970-01-01
        • 2021-12-08
        • 1970-01-01
        • 2019-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多