SELECT SUM(order_amount) AS total_order_amount, COUNT(*) AS total_order_count, SUM(CASE WHEN order_time >= 'yyyy-mm-01'::date AND order_time < ('yyyy-mm-01'::date + INTERVAL '1 month') THEN order_amount ELSE 0 END) AS order_amount_month, SUM(CASE WHEN order_time >= 'yyyy-01-01'::date AND order_time < ('yyyy-01-01'::date + INTERVAL '1 year') THEN order_amount ELSE 0 END) AS order_amount_year, SUM(CASE WHEN order_time >= 'yyyy-01-01'::date AND order_time < ('yyyy-01-01'::date + INTERVAL '1 year') THEN 1 ELSE 0 END) AS order_count_year FROM ( SELECT * FROM order_1 WHERE member_id = :memberId AND order_time >= 'yyyy-01-01'::date UNION ALL SELECT * FROM order_2 WHERE member_id = :memberId AND order_time >= 'yyyy-01-01'::date UNION ALL SELECT * FROM order_3 WHERE member_id = :memberId AND order_time >= 'yyyy-01-01'::date ) AS all_orders
时间: 2024-04-21 14:29:32 浏览: 133
Like-and-OrderBy.rar_sql like order by
这是一个 SQL 查询语句,用于查询某个会员在一年内的订单数据。其中,total_order_amount 表示该会员所有订单的总金额,total_order_count 表示该会员所有订单的总数量,order_amount_month 表示该会员当月订单的总金额,order_amount_year 表示该会员一年内订单的总金额,order_count_year 表示该会员一年内的订单总数量。这个查询语句通过 UNION ALL 将三张订单表合并,并且使用 CASE WHEN 判断订单的时间范围,然后进行相应的求和操作。其中,:memberId 是一个占位符,需要传入具体的会员 ID 才能完成查询。
阅读全文