我正在尝试找出客户重复购买所需的平均天数。
这就是我拥有的
Email_address,
Order_name,
Date,
Order_number,
lead(date) over (partition by email_address order by Order_number) next_order_date,
lead(Order_number) over (partition by email_address order by Order_number) next_order_number
FROM `dbOrders`
Group by 1,2,3,4
正在退货
[
{
"Email_address": "email@test.com",
"Order_name": "#1234",
"Date": "2019-11-08 00:18:00 UTC",
"Order_number": "1",
"next_order_date": "2019-11-28 20:54:00 UTC",
"next_order_number": "2"
},
我现在想获得所有第一次购买和第二次购买之间的平均天数,然后是第二次和第三次购买之间的平均天数
样本数据集
|---------------------|------------------|------------------|------------------|
| Email_address | Order_name | Order_number | Date |
|---------------------|------------------|------------------|------------------|
| test@test.com | 364 | 1 | 2019-08-11 |
|---------------------|------------------|------------------|------------------|
| test@test.com | 366 | 2 | 2019-08-29 |
|---------------------|------------------|------------------|------------------|
| j@test.com | 495 | 1 | 2019-09-04 |
|---------------------|------------------|------------------|------------------|
| j@test.com | 564 | 2 | 2019-10-04 |
|---------------------|------------------|------------------|------------------|
我想找到两个客户的订单之间的平均时间差。 test@test.com = 18天 j@test.com = 30天
平均= 24天
答案 0 :(得分:1)
您似乎要查询汇总查询:
select seqnum,
avg(date_diff(date, next_date, day)) as avg_days_to_next_order
from (select o.*,
row_number() over (partition by email_address order by date) as seqnum,
lead(date) over (partition by email_address order by date) as next-date
from `dbOrders` o
) o
group by seqnum
order by seqnum;