编写SQL脚本,基于订单数据计算22年双十一活动期间新客和老客的购买人数、支付金额、各个品类及货品购买人数和购买金额;其中新客指22年10月30日前365天无购买行为消费者;老客指22年10月30日前365天有购买行为消费者,且在D11有购买行为
时间: 2024-01-24 10:17:33 浏览: 280
以下是基于订单数据计算22年双十一活动期间新客和老客的购买人数、支付金额、各个品类及货品购买人数和购买金额的SQL脚本:
```
-- 计算双十一新客和老客购买人数和支付金额
SELECT
SUM(CASE WHEN order_time >= '2022-11-11 00:00:00' AND is_new_customer = 1 THEN 1 ELSE 0 END) AS new_customer_count,
SUM(CASE WHEN order_time >= '2022-11-11 00:00:00' AND is_new_customer = 0 THEN 1 ELSE 0 END) AS old_customer_count,
SUM(CASE WHEN order_time >= '2022-11-11 00:00:00' AND is_new_customer = 1 THEN payment_amount ELSE 0 END) AS new_customer_payment_amount,
SUM(CASE WHEN order_time >= '2022-11-11 00:00:00' AND is_new_customer = 0 THEN payment_amount ELSE 0 END) AS old_customer_payment_amount
FROM orders
WHERE order_time >= '2022-11-11 00:00:00' OR order_time < '2022-10-30 00:00:00';
-- 计算双十一新客和老客购买各个品类的人数和金额
SELECT
category,
SUM(CASE WHEN order_time >= '2022-11-11 00:00:00' AND is_new_customer = 1 THEN 1 ELSE 0 END) AS new_customer_count,
SUM(CASE WHEN order_time >= '2022-11-11 00:00:00' AND is_new_customer = 0 THEN 1 ELSE 0 END) AS old_customer_count,
SUM(CASE WHEN order_time >= '2022-11-11 00:00:00' AND is_new_customer = 1 THEN payment_amount ELSE 0 END) AS new_customer_payment_amount,
SUM(CASE WHEN order_time >= '2022-11-11 00:00:00' AND is_new_customer = 0 THEN payment_amount ELSE 0 END) AS old_customer_payment_amount
FROM orders
WHERE (order_time >= '2022-11-11 00:00:00' OR order_time < '2022-10-30 00:00:00') AND is_purchased = 1
GROUP BY category;
-- 计算双十一新客和老客购买每个货品的人数和金额
SELECT
product_id,
SUM(CASE WHEN order_time >= '2022-11-11 00:00:00' AND is_new_customer = 1 THEN 1 ELSE 0 END) AS new_customer_count,
SUM(CASE WHEN order_time >= '2022-11-11 00:00:00' AND is_new_customer = 0 THEN 1 ELSE 0 END) AS old_customer_count,
SUM(CASE WHEN order_time >= '2022-11-11 00:00:00' AND is_new_customer = 1 THEN payment_amount ELSE 0 END) AS new_customer_payment_amount,
SUM(CASE WHEN order_time >= '2022-11-11 00:00:00' AND is_new_customer = 0 THEN payment_amount ELSE 0 END) AS old_customer_payment_amount
FROM order_items
JOIN orders ON order_items.order_id = orders.order_id
WHERE (orders.order_time >= '2022-11-11 00:00:00' OR orders.order_time < '2022-10-30 00:00:00') AND orders.is_purchased = 1
GROUP BY product_id;
```
其中,`orders` 表包含订单的基本信息,如订单ID、订单时间、支付金额、是否购买等;`order_items` 表包含订单中每个货品的信息,如货品ID、购买数量、价格等。在以上SQL脚本中,我们根据订单时间和购买行为(新客或老客)对订单数据进行筛选,并使用聚合函数 `SUM` 和 `GROUP BY` 对数据进行统计和分组。
阅读全文