BigQueryで日毎のユニークユーザ数と通算のユニークユーザ数を集計したい

実現したいこと

下記のtable_Aから、

user_id date
1 2023-01-01
2 2023-01-01
1 2023-01-02
3 2023-01-02
1 2023-01-03
3 2023-01-03

下記のように集計したいです。

date 日毎のUU 通算のUU
2023-01-01 2 2
2023-01-02 2 3
2023-01-03 2 3

クエリを書いてみたのですが下記ですと毎日のUUの積み重ねになってしまいます。

select date, count(distinct user_id) as 毎日のUU, sum(count(distinct user_id)) over(order by date rows unbounded preceding) as 通算のUU from table_A group by date

相関サブクエリを使って、

select date, count(distinct user_id) as 毎日のUU, sum(count(distinct user_id)) over(order by date rows unbounded preceding) as 通算のUU from table_A as x inner join table_A as y where x.date >= y.date group by date

とすると通算の方は欲しい数字になるのですが、毎日のUUの方の数字が少なくなってしまいます。

毎日のUUと通算のUUを別々に集計して、dateでinner joinして一つのテーブルにするしか方法はないでしょうか。

コメントを投稿

0 コメント