编写MYSQL脚本,计算近三个月消费者购买人数,购买金额,复购人数,复购金额,复购消费者支付金额
时间: 2023-06-20 11:08:25 浏览: 67
假设有一个订单表order,包含以下字段:order_id(订单ID),customer_id(消费者ID),order_time(订单时间),order_amount(订单金额)
可以使用以下MYSQL脚本计算近三个月的相关数据:
```
SELECT
COUNT(DISTINCT customer_id) AS total_customer,
SUM(order_amount) AS total_amount,
COUNT(DISTINCT CASE WHEN order_time >= DATE_SUB(NOW(), INTERVAL 3 MONTH) THEN customer_id END) AS repeat_customer,
SUM(CASE WHEN order_time >= DATE_SUB(NOW(), INTERVAL 3 MONTH) THEN order_amount ELSE 0 END) AS repeat_amount,
SUM(CASE WHEN order_time >= DATE_SUB(NOW(), INTERVAL 3 MONTH) THEN order_amount ELSE 0 END) / COUNT(DISTINCT CASE WHEN order_time >= DATE_SUB(NOW(), INTERVAL 3 MONTH) THEN customer_id END) AS repeat_avg_amount
FROM
order
WHERE
order_time >= DATE_SUB(NOW(), INTERVAL 3 MONTH);
```
这个脚本会返回以下五个值:
- total_customer:近三个月的消费者购买人数
- total_amount:近三个月的购买金额
- repeat_customer:近三个月的复购人数
- repeat_amount:近三个月的复购金额
- repeat_avg_amount:近三个月的复购消费者平均支付金额
阅读全文