繁体   English   中英

如何获取累计用户总数但忽略前一天已经出现的用户? 使用大查询

[英]How to get cumulative total users but ignoring the users who already appear in previous day? using bigquery

所以我想计算每天的累积用户,但如果用户存在是前几天,他们将不计算在内。

date_key      user_id
2022-01-01     001
2022-01-01     002
2022-01-02     001
2022-01-02     003
2022-01-03     002
2022-01-03     003
2022-01-04     002
2022-01-04     004

每天我们可以得到

date_key     total_user
2022-01-01      2
2022-01-02      2
2022-01-03      2
2022-01-04      2

如果我们简单地计算累积,我们每天可以得到 2,4,6,8 目标是得到这样的表格

date_key     total_user
2022-01-01      2
2022-01-02      3
2022-01-03      3
2022-01-04      4

我使用这个查询来获得结果,因为数据真的很大。 查询需要永远完成。

select b.date_key,count(distinct a.user_id) total_user
from t1 a
join t1 b 
   on b.date_key >= a.date_key 
   and date_trunc(a.date_key,month) = date_trunc(b.date_key,month)
group by 1
order by 1

是的,当月份变化时,计算应该重置。

顺便说一句,我正在使用谷歌 bigquery

按日期顺序对每个用户的外观进行编号。 只计算第一次看到的那些:

with data as (
    select *,
        row_number() over (partition by date_trunc(date_key, month), userid
                           order by date_key) as rn
    from T
)
select date_key,
    sum(count(case when rn = 1 then 1 end)) -- or countif(rn = 1)
        over (partition by date_trunc(date_key, month)
              order by date_key) as cum_monthly_users
from data
group by date_key;

https://dbfiddle.uk/?rdbms=postgres_14&fiddle=dc426d79a7786fc8a5b25a22f0755e27

  1. 累计用户总数,但忽略前一天已经出现的用户?
  2. 当月份变化时,计算应该重置
  3. 数据真的很大

考虑以下方法

select date_key, 
  ( select hll_count.merge(u) 
    from unnest(users) u
  ) as total_user
from (
  select date_key, date_trunc(date(date_key), month) year_month,
    array_agg(users) over(partition by date_trunc(date(date_key), month) order by date_key) users
  from (
    select date_key, hll_count.init(user_id) users
    from your_table
    group by date_key
  )
)      

如果应用于您问题中的示例数据 - output 是

在此处输入图像描述

注意:没有[明显]上面的##1和2被满足-和output如预期的那样,但在这里我们使用HyperLogLog++函数将有效地解决上面的#3

HLL++ 函数是近似聚合函数。 与精确聚合函数(如 COUNT(DISTINCT))相比,近似聚合通常需要更少的 memory,但也会引入统计错误。 这使得 HLL++ 函数适用于线性 memory 使用不切实际的大型数据流,以及已经近似的数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM