写一个sql 根据传递过来的参数 Date类型的某年某月 和memberId 在三张订单表里面获取数据 回显的数据要有 会员所有的订单金额,会员所有的总消费次数 并且还要获得会员在不同订单表的消费总金额 数据库使用的postgresql
时间: 2024-03-14 11:43:41 浏览: 118
订单管理数据库(sql)
以下是根据传递过来的参数Date类型的某年某月和memberId在三张订单表里面获取数据的 SQL 查询语句:
```
SELECT
SUM(order_amount) AS total_order_amount,
COUNT(*) AS total_order_count,
SUM(CASE WHEN order_time::date >= date_trunc('month', :dateParam)::date AND order_time::date < (date_trunc('month', :dateParam) + INTERVAL '1 month')::date THEN order_amount ELSE 0 END) AS order_amount_month,
SUM(CASE WHEN order_time::date >= date_trunc('year', :dateParam)::date AND order_time::date < (date_trunc('year', :dateParam) + INTERVAL '1 year')::date THEN order_amount ELSE 0 END) AS order_amount_year,
SUM(CASE WHEN order_time::date >= date_trunc('year', :dateParam)::date AND order_time::date < (date_trunc('year', :dateParam) + INTERVAL '1 year')::date THEN 1 ELSE 0 END) AS order_count_year,
'order_1' AS order_table
FROM
order_1
WHERE
member_id = :memberId AND order_time::date >= date_trunc('year', :dateParam)::date AND order_time::date < (date_trunc('year', :dateParam) + INTERVAL '1 year')::date
UNION ALL
SELECT
SUM(order_amount) AS total_order_amount,
COUNT(*) AS total_order_count,
SUM(CASE WHEN order_time::date >= date_trunc('month', :dateParam)::date AND order_time::date < (date_trunc('month', :dateParam) + INTERVAL '1 month')::date THEN order_amount ELSE 0 END) AS order_amount_month,
SUM(CASE WHEN order_time::date >= date_trunc('year', :dateParam)::date AND order_time::date < (date_trunc('year', :dateParam) + INTERVAL '1 year')::date THEN order_amount ELSE 0 END) AS order_amount_year,
SUM(CASE WHEN order_time::date >= date_trunc('year', :dateParam)::date AND order_time::date < (date_trunc('year', :dateParam) + INTERVAL '1 year')::date THEN 1 ELSE 0 END) AS order_count_year,
'order_2' AS order_table
FROM
order_2
WHERE
member_id = :memberId AND order_time::date >= date_trunc('year', :dateParam)::date AND order_time::date < (date_trunc('year', :dateParam) + INTERVAL '1 year')::date
UNION ALL
SELECT
SUM(order_amount) AS total_order_amount,
COUNT(*) AS total_order_count,
SUM(CASE WHEN order_time::date >= date_trunc('month', :dateParam)::date AND order_time::date < (date_trunc('month', :dateParam) + INTERVAL '1 month')::date THEN order_amount ELSE 0 END) AS order_amount_month,
SUM(CASE WHEN order_time::date >= date_trunc('year', :dateParam)::date AND order_time::date < (date_trunc('year', :dateParam) + INTERVAL '1 year')::date THEN order_amount ELSE 0 END) AS order_amount_year,
SUM(CASE WHEN order_time::date >= date_trunc('year', :dateParam)::date AND order_time::date < (date_trunc('year', :dateParam) + INTERVAL '1 year')::date THEN 1 ELSE 0 END) AS order_count_year,
'order_3' AS order_table
FROM
order_3
WHERE
member_id = :memberId AND order_time::date >= date_trunc('year', :dateParam)::date AND order_time::date < (date_trunc('year', :dateParam) + INTERVAL '1 year')::date;
```
其中,:dateParam和:memberId是占位符,需要传入具体的日期和会员ID才能完成查询。以上查询语句会返回一个结果集,包含了会员的所有订单金额、总消费次数以及在不同订单表中的消费总金额。
需要注意的是,以上查询语句中用到了 PostgreSQL 特定的日期函数和语法,如 date_trunc 函数用于取某个日期的年份和月份的起始日期,::date 语法用于将 timestamp 类型转换为 date 类型。
阅读全文