编写SQL脚本,仅基于订单数据表计算22年10月31日-22年11月11日活动期间新客和老客的购买人数、支付金额、各个品类及货品购买人数和购买金额;其中新客指22年10月30日前365天无购买行为消费者;老客指22年10月30日前365天有购买行为消费者,且在D11有购买行为
时间: 2024-01-24 14:17:35 浏览: 71
根据题意,我们可以先定义出新客和老客的条件:
新客:22年10月30日前365天无购买行为消费者
老客:22年10月30日前365天有购买行为消费者,且在D11有购买行为
然后,我们可以使用以下SQL脚本计算活动期间的数据:
```
SELECT
SUM(CASE WHEN order_date BETWEEN '2022-10-31' AND '2022-11-11' AND is_new_customer = 1 THEN 1 ELSE 0 END) AS new_customer_count,
SUM(CASE WHEN order_date BETWEEN '2022-10-31' AND '2022-11-11' AND is_new_customer = 0 THEN 1 ELSE 0 END) AS old_customer_count,
SUM(CASE WHEN order_date BETWEEN '2022-10-31' AND '2022-11-11' AND is_new_customer = 1 THEN payment_amount ELSE 0 END) AS new_customer_payment_amount,
SUM(CASE WHEN order_date BETWEEN '2022-10-31' AND '2022-11-11' AND is_new_customer = 0 THEN payment_amount ELSE 0 END) AS old_customer_payment_amount,
SUM(CASE WHEN order_date BETWEEN '2022-10-31' AND '2022-11-11' THEN CASE WHEN category = 'A' THEN 1 ELSE 0 END ELSE 0 END) AS category_A_customer_count,
SUM(CASE WHEN order_date BETWEEN '2022-10-31' AND '2022-11-11' THEN CASE WHEN category = 'A' THEN payment_amount ELSE 0 END ELSE 0 END) AS category_A_payment_amount,
SUM(CASE WHEN order_date BETWEEN '2022-10-31' AND '2022-11-11' THEN CASE WHEN category = 'B' THEN 1 ELSE 0 END ELSE 0 END) AS category_B_customer_count,
SUM(CASE WHEN order_date BETWEEN '2022-10-31' AND '2022-11-11' THEN CASE WHEN category = 'B' THEN payment_amount ELSE 0 END ELSE 0 END) AS category_B_payment_amount,
SUM(CASE WHEN order_date BETWEEN '2022-10-31' AND '2022-11-11' THEN CASE WHEN product_id = 'P1' THEN 1 ELSE 0 END ELSE 0 END) AS product_P1_customer_count,
SUM(CASE WHEN order_date BETWEEN '2022-10-31' AND '2022-11-11' THEN CASE WHEN product_id = 'P1' THEN payment_amount ELSE 0 END ELSE 0 END) AS product_P1_payment_amount
FROM
orders
WHERE
(order_date BETWEEN '2022-10-31' AND '2022-11-11') OR (order_date BETWEEN '2021-10-31' AND '2022-10-30' AND order_date NOT IN (SELECT order_date FROM orders WHERE order_date BETWEEN '2022-10-31' AND '2022-11-11')) OR (order_date BETWEEN '2021-11-12' AND '2022-10-30' AND order_date IN (SELECT order_date FROM orders WHERE order_date BETWEEN '2022-10-31' AND '2022-11-11'))
```
其中,`orders`为订单数据表,包含以下字段:
- `order_id`: 订单编号
- `order_date`: 下单日期
- `customer_id`: 客户编号
- `product_id`: 产品编号
- `category`: 产品类别
- `payment_amount`: 支付金额
SQL脚本中的`is_new_customer`字段表示是否为新客,其定义如下:
```
CASE
WHEN customer_id IN (SELECT customer_id FROM orders WHERE order_date BETWEEN '2021-10-31' AND '2022-10-30') AND customer_id NOT IN (SELECT customer_id FROM orders WHERE order_date BETWEEN '2022-10-31' AND '2022-11-11') THEN 1
ELSE 0
END AS is_new_customer
```
其中,`is_new_customer`的值为1表示是新客,值为0表示是老客。
阅读全文