WITH all_orders AS ( SELECT A .create_time AS TIME, A.paid_amount AS amount, 'flower_t_order' AS order_table_name FROM flower_t_order A WHERE A.member_id = 1 AND EXTRACT ( YEAR FROM A.create_time ) = 2023 UNION ALL SELECT B.create_time AS TIME, B.paid_amount AS amount, 'shop_t_order' AS order_table_name FROM shop_t_order B WHERE B.member_id = 1 AND EXTRACT ( YEAR FROM B.create_time ) = 2023 UNION ALL SELECT C .create_time AS TIME, C.paid_amount AS amount, 'ticket_t_order' AS order_table_name FROM ticket_t_order C WHERE C.member_id = 1 AND EXTRACT ( YEAR FROM b.create_time ) = 2023 ) SELECT EXTRACT ( MONTH FROM all_orders."time" ( TIMESTAMP ) ) AS MONTH, SUM ( all_orders.amount ) AS total_order_amount, COUNT ( * ) AS total_order_count, SUM ( CASE WHEN order_table_name = 'flower_t_order' THEN paid_amount ELSE 0 END ) AS order_table_1_amount, SUM ( CASE WHEN order_table_name = 'shop_t_order' THEN paid_amount ELSE 0 END ) AS order_table_2_amount, SUM ( CASE WHEN order_table_name = 'ticket_t_order' THEN paid_amount ELSE 0 END ) AS order_table_3_amount FROM all_orders GROUP BY MONTH ORDER BY MONTH;
时间: 2024-04-21 11:30:30 浏览: 101
String_Builder_Class_and_Demonstration.zip_class A
这个查询语句的问题在于在第 20 行中引用了表别名 "b",但是它在此之前并没有被定义,应该使用表别名 "C"。请将第 20 行中的 "b" 修改为 "C",应该可以解决这个错误。修改后的查询语句如下:
WITH all_orders AS (
SELECT A.create_time AS TIME, A.paid_amount AS amount, 'flower_t_order' AS order_table_name
FROM flower_t_order A
WHERE A.member_id = 1 AND EXTRACT ( YEAR FROM A.create_time ) = 2023
UNION ALL
SELECT B.create_time AS TIME, B.paid_amount AS amount, 'shop_t_order' AS order_table_name
FROM shop_t_order B
WHERE B.member_id = 1 AND EXTRACT ( YEAR FROM B.create_time ) = 2023
UNION ALL
SELECT C.create_time AS TIME, C.paid_amount AS amount, 'ticket_t_order' AS order_table_name
FROM ticket_t_order C
WHERE C.member_id = 1 AND EXTRACT ( YEAR FROM C.create_time ) = 2023
)
SELECT EXTRACT ( MONTH FROM all_orders."time" ( TIMESTAMP ) ) AS MONTH,
SUM ( all_orders.amount ) AS total_order_amount,
COUNT ( * ) AS total_order_count,
SUM ( CASE WHEN order_table_name = 'flower_t_order' THEN paid_amount ELSE 0 END ) AS order_table_1_amount,
SUM ( CASE WHEN order_table_name = 'shop_t_order' THEN paid_amount ELSE 0 END ) AS order_table_2_amount,
SUM ( CASE WHEN order_table_name = 'ticket_t_order' THEN paid_amount ELSE 0 END ) AS order_table_3_amount
FROM all_orders
GROUP BY MONTH
ORDER BY MONTH;
阅读全文