BigQuery:滚动每日计数访问者的付款摘要
BigQuery: Rolling daily count visitor's summary of payment
我有这个数据:
date
visitor_id
total_payment
2022-01-01
A
20
2022-01-01
B
15
2022-01-01
C
20
2022-01-02
B
10
2022-01-02
D
25
我想统计 total_payment 等于或大于 20 美元的每日访客人数,话虽如此,我希望的结果是:
date
count_visitor
2022-01-01
2
2022-01-02
4
2022-01-01 是 2,因为只有 A 和 C 的付款超过 20 美元,但是在 2022-01-02 又增加了 2,因为 B 是 35 美元(总和),D 是 25 美元。
是否有任何可能的查询?我希望我的描述很清楚。提前谢谢你。
欢迎@Indri
下面的查询将为您提供每天 运行 行的总和,其中 total_amount
大于等于 20,我相信这应该会给您想要的答案:
WITH data AS(
SELECT "2022-01-01" AS date, "A" AS visitor_id, 20 AS total_payment
UNION ALL
SELECT "2022-01-01" AS date, "B" AS visitor_id, 15 AS total_payment
UNION ALL
SELECT "2022-01-01" AS date, "C" AS visitor_id, 20 AS total_payment
UNION ALL
SELECT "2022-01-02" AS date, "A" AS visitor_id, 10 AS total_payment
UNION ALL
SELECT "2022-01-02" AS date, "D" AS visitor_id, 25 AS total_payment
)
SELECT
*,
COUNT(*) OVER(ORDER BY date)
FROM data
WHERE total_payment >= 20
您可以使用此查询作为解决方案。
首先,我计算每个用户的累计付款。
然后,我找到每个用户超过 20$ 累计付款的最短日期。
在最后一步,我计算每个最短日期的用户数,并累加该数字。
在输出中你不必有 first_day_users 列,但我保留它是为了更容易理解代码。
所以输出看起来像这样:
WITH
data AS(
SELECT "2022-01-01" AS date, "A" AS visitor_id, 20 AS total_payment UNION ALL
SELECT "2022-01-01" AS date, "B" AS visitor_id, 15 AS total_payment UNION ALL
SELECT "2022-01-01" AS date, "C" AS visitor_id, 20 AS total_payment UNION ALL
SELECT "2022-01-02" AS date, "B" AS visitor_id, 10 AS total_payment UNION ALL
SELECT "2022-01-02" AS date, "D" AS visitor_id, 25 AS total_payment
),
user_cumulatives as
(
SELECT
visitor_id,
date,
SUM(total_payment) OVER (PARTITION BY visitor_id ORDER BY date) as cumulative_payment
FROM data
),
user_first_dates as
(
select visitor_id, min(date) as date
from user_cumulatives
where cumulative_payment >= 20
group by 1
)
select date, count(*) as first_day_users, sum(count(*)) over (order by date) as count_visitor
from user_first_dates
group by 1
order by date
我有这个数据:
date | visitor_id | total_payment |
---|---|---|
2022-01-01 | A | 20 |
2022-01-01 | B | 15 |
2022-01-01 | C | 20 |
2022-01-02 | B | 10 |
2022-01-02 | D | 25 |
我想统计 total_payment 等于或大于 20 美元的每日访客人数,话虽如此,我希望的结果是:
date | count_visitor |
---|---|
2022-01-01 | 2 |
2022-01-02 | 4 |
2022-01-01 是 2,因为只有 A 和 C 的付款超过 20 美元,但是在 2022-01-02 又增加了 2,因为 B 是 35 美元(总和),D 是 25 美元。
是否有任何可能的查询?我希望我的描述很清楚。提前谢谢你。
欢迎@Indri
下面的查询将为您提供每天 运行 行的总和,其中 total_amount
大于等于 20,我相信这应该会给您想要的答案:
WITH data AS(
SELECT "2022-01-01" AS date, "A" AS visitor_id, 20 AS total_payment
UNION ALL
SELECT "2022-01-01" AS date, "B" AS visitor_id, 15 AS total_payment
UNION ALL
SELECT "2022-01-01" AS date, "C" AS visitor_id, 20 AS total_payment
UNION ALL
SELECT "2022-01-02" AS date, "A" AS visitor_id, 10 AS total_payment
UNION ALL
SELECT "2022-01-02" AS date, "D" AS visitor_id, 25 AS total_payment
)
SELECT
*,
COUNT(*) OVER(ORDER BY date)
FROM data
WHERE total_payment >= 20
您可以使用此查询作为解决方案。 首先,我计算每个用户的累计付款。 然后,我找到每个用户超过 20$ 累计付款的最短日期。 在最后一步,我计算每个最短日期的用户数,并累加该数字。 在输出中你不必有 first_day_users 列,但我保留它是为了更容易理解代码。
所以输出看起来像这样:
WITH
data AS(
SELECT "2022-01-01" AS date, "A" AS visitor_id, 20 AS total_payment UNION ALL
SELECT "2022-01-01" AS date, "B" AS visitor_id, 15 AS total_payment UNION ALL
SELECT "2022-01-01" AS date, "C" AS visitor_id, 20 AS total_payment UNION ALL
SELECT "2022-01-02" AS date, "B" AS visitor_id, 10 AS total_payment UNION ALL
SELECT "2022-01-02" AS date, "D" AS visitor_id, 25 AS total_payment
),
user_cumulatives as
(
SELECT
visitor_id,
date,
SUM(total_payment) OVER (PARTITION BY visitor_id ORDER BY date) as cumulative_payment
FROM data
),
user_first_dates as
(
select visitor_id, min(date) as date
from user_cumulatives
where cumulative_payment >= 20
group by 1
)
select date, count(*) as first_day_users, sum(count(*)) over (order by date) as count_visitor
from user_first_dates
group by 1
order by date