写一个sql 根据传递过来的参数 Date类型的某年某月 和memberId 在三张订单表里面获取数据 回的数据要有 总的订单金额 每个月的订单金额 消费次数 数据库使用的postgresql
时间: 2024-03-14 11:43:29 浏览: 78
您好,以下是根据传递过来的参数 Date类型的某年某月和memberId,在三张订单表里面获取数据的SQL语句,回的数据包括总的订单金额、每个月的订单金额、消费次数:
```
SELECT
SUM(total_amount) AS total_amount,
DATE_TRUNC('month', order_time) AS month,
COUNT(*) AS order_count
FROM
(
SELECT total_amount, order_time FROM order_table1 WHERE EXTRACT(YEAR FROM order_time) = EXTRACT(YEAR FROM :date) AND EXTRACT(MONTH FROM order_time) = EXTRACT(MONTH FROM :date) AND member_id = :memberId
UNION ALL
SELECT total_amount, order_time FROM order_table2 WHERE EXTRACT(YEAR FROM order_time) = EXTRACT(YEAR FROM :date) AND EXTRACT(MONTH FROM order_time) = EXTRACT(MONTH FROM :date) AND member_id = :memberId
UNION ALL
SELECT total_amount, order_time FROM order_table3 WHERE EXTRACT(YEAR FROM order_time) = EXTRACT(YEAR FROM :date) AND EXTRACT(MONTH FROM order_time) = EXTRACT(MONTH FROM :date) AND member_id = :memberId
) AS orders
GROUP BY
DATE_TRUNC('month', order_time)
```
其中,`:date` 和 `:memberId` 是您传递过来的参数,可以根据实际情况进行修改。这段 SQL 语句会返回每个月的订单总金额、该月订单数。
阅读全文