优化代码,并提供新脚本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'))
时间: 2024-01-24 20:17:58 浏览: 60
MySql培训日志_stu
可以将重复的子查询合并,并使用JOIN操作,同时使用IF语句替代CASE语句,可以简化代码并提高效率。下面是优化后的脚本:
SELECT
SUM(IF(order_date BETWEEN '2022-10-31' AND '2022-11-11' AND is_new_customer = 1, 1, 0)) AS new_customer_count,
SUM(IF(order_date BETWEEN '2022-10-31' AND '2022-11-11' AND is_new_customer = 0, 1, 0)) AS old_customer_count,
SUM(IF(order_date BETWEEN '2022-10-31' AND '2022-11-11' AND is_new_customer = 1, payment_amount, 0)) AS new_customer_payment_amount,
SUM(IF(order_date BETWEEN '2022-10-31' AND '2022-11-11' AND is_new_customer = 0, payment_amount, 0)) AS old_customer_payment_amount,
SUM(IF(order_date BETWEEN '2022-10-31' AND '2022-11-11' AND category = 'A', 1, 0)) AS category_A_customer_count,
SUM(IF(order_date BETWEEN '2022-10-31' AND '2022-11-11' AND category = 'A', payment_amount, 0)) AS category_A_payment_amount,
SUM(IF(order_date BETWEEN '2022-10-31' AND '2022-11-11' AND category = 'B', 1, 0)) AS category_B_customer_count,
SUM(IF(order_date BETWEEN '2022-10-31' AND '2022-11-11' AND category = 'B', payment_amount, 0)) AS category_B_payment_amount,
SUM(IF(order_date BETWEEN '2022-10-31' AND '2022-11-11' AND product_id = 'P1', 1, 0)) AS product_P1_customer_count,
SUM(IF(order_date BETWEEN '2022-10-31' AND '2022-11-11' AND product_id = 'P1', payment_amount, 0)) AS product_P1_payment_amount
FROM
orders o
LEFT JOIN
(SELECT DISTINCT order_date FROM orders WHERE order_date BETWEEN '2022-10-31' AND '2022-11-11') d1 ON o.order_date = d1.order_date
LEFT JOIN
(SELECT DISTINCT order_date FROM orders WHERE 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')) d2 ON o.order_date = d2.order_date
LEFT JOIN
(SELECT DISTINCT order_date FROM orders WHERE 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')) d3 ON o.order_date = d3.order_date
WHERE
d1.order_date IS NOT NULL OR d2.order_date IS NOT NULL OR d3.order_date IS NOT NULL;
阅读全文