我目前正在处理查询,我想计算历史上有多少客户向我们下了订单,但当月没有下过订单
最终输出应该是这样的
月 | 流失的客户 |
---|---|
一月 | 23 |
二月 | 32 |
第一行的 23 号是指所有在 12 月之前至少下了一个订单但在一月份没有下过订单的客户,而 32 是指有多少客户在 1 月之前至少下过一个订单但在 2 月没有下过订单
目前我拥有的数据是 客户 ID、交货日期(日期时间戳格式)、订单 ID 我正在使用 Google Bigqeury 平台。
直到现在我才想到这个
DATE_TRUNC((delivery_date),MONTH) AS Month, customer_id, COUNT(DISTINCT order_id) AS orders FROM orders WHERE LOWER(order_status) = 'delivered' GROUP BY 1,2 )
我在这里创建了一个包含示例数据的模拟表:https://docs.google.com/spreadsheets/d/1TWLM7U3s_4ZoVPmB_oCJ39LtHkgzxlXNA-luxZbHvyA/edit#gid=0
使用此数据和下面的查询来获取客户总数的运行计数。
简单查询:
WITH
table_a AS (
SELECT
customer_id,
delivery_date_ AS delivery_date,
EXTRACT(month
FROM
delivery_date_) AS month_int,
FORMAT_DATE('%B', delivery_date_) AS Month,
EXTRACT(year
FROM
delivery_date_) AS year_int,
order_id
FROM
`optimal-weft-418017.78241441.78241441_01` ),
table_b AS (
SELECT
COUNT(customer_id) customer_count,
month,
month_int
FROM
table_a
GROUP BY
month,
month_int)
SELECT
month,
customer_count AS distinct_customer_count_for_month,
IFNULL(SUM(customer_count) OVER(ORDER BY month_int ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING),0) AS Churned_Customers
FROM
table_b
ORDER BY
month_int
本质上是每月获取客户数量,并对之前所有月份进行累计。
输出: |月|distinct_customer_count_for_month|流失的客户| |--------------------|---------------------|----- ----------------| |一月|1|0| |三月|1|1| |五月|8|2| |六月|6|10| |八月|3|16| |十月|1|19| |十一月|1|20| |12 月|2|21|
根据输入的日期范围动态查询每个月的客户数为 0 的月份。与上面的逻辑相同,但使用日期数组:
WITH
table_a AS (
SELECT
customer_id,
delivery_date_ AS delivery_date,
EXTRACT(month
FROM
delivery_date_) AS month_int,
FORMAT_DATE('%B', delivery_date_) AS Month,
EXTRACT(year
FROM
delivery_date_) AS year_int,
order_id
FROM
`optimal-weft-418017.78241441.78241441_01` ),
date_array AS (
SELECT
date
FROM (
SELECT
NULL)
LEFT JOIN
UNNEST (GENERATE_DATE_ARRAY("2022-01-01", "2024-01-01", INTERVAL 1 month) )AS date ),
table_c AS (
SELECT
*,
EXTRACT(month
FROM
date) AS month_int_template,
FORMAT_DATE('%B', date) AS Month_template,
FROM
date_array
ORDER BY
date),
table_d AS (
SELECT
*
FROM
table_c AS c
LEFT JOIN
table_a a
ON
c.date = EXTRACT(date
FROM
a.delivery_date) ),
table_e AS (
SELECT
*EXCEPT(order_id,
customer_id),
COUNT(customer_id) customer_count
FROM
table_d
GROUP BY
1,2,3,4,5,6,7
ORDER BY
1 )
SELECT
date,
month_template,
IFNULL(SUM(customer_count) OVER(ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING),0) AS Churned_Customers
FROM
table_e
ORDER BY 1
输出:
日期 | 月份模板 | 流失的客户 |
---|---|---|
2022-01-01 | 一月 | 0 |
2022-02-01 | 二月 | 0 |
2022-03-01 | 三月 | 0 |
2022-04-01 | 四月 | 0 |
2022-05-01 | 五月 | 0 |
2022-06-01 | 六月 | 0 |
2022-07-01 | 七月 | 0 |
2022-08-01 | 八月 | 0 |
2022-09-01 | 九月 | 0 |
2022-10-01 | 十月 | 0 |
2022-11-01 | 十一月 | 0 |
2022-12-01 | 十二月 | 0 |
2023-01-01 | 一月 | 0 |
2023-02-01 | 二月 | 1 |
2023-03-01 | 三月 | 1 |
2023-04-01 | 四月 | 2 |
2023-05-01 | 五月 | 2 |
2023-06-01 | 六月 | 10 |
2023-07-01 | 七月 | 16 |
2023-08-01 | 八月 | 16 |
2023-09-01 | 九月 | 19 |
2023-10-01 | 十月 | 19 |
2023-11-01 | 十一月 | 20 |
2023-12-01 | 十二月 | 21 |
2024-01-01 | 一月 | 23 |
如果您想要每月的不同计数,则需要编辑查询。谢谢。