LeetCode 1205 每月交易2(PostgreSQL)

数据准备

create type state as enum('approved', 'declined');
create table Transactions(
id int, 
country varchar(4),
state_enum state,
amount int,
trans_date date
);
Create table If Not Exists Chargebacks (
trans_id int, 
trans_date date
);
insert into Transactions (id, country, state_enum, amount, trans_date) values ('101', 'US', 'approved', '1000', '2019-05-18');
insert into Transactions (id, country, state_enum, amount, trans_date) values ('102', 'US', 'declined', '2000', '2019-05-19');
insert into Transactions (id, country, state_enum, amount, trans_date) values ('103', 'US', 'approved', '3000', '2019-06-10');
insert into Transactions (id, country, state_enum, amount, trans_date) values ('104', 'US', 'declined', '4000', '2019-06-13');
insert into Transactions (id, country, state_enum, amount, trans_date) values ('105', 'US', 'approved', '5000', '2019-06-15');
Truncate table Chargebacks;
insert into Chargebacks (trans_id, trans_date) values ('102', '2019-05-29');
insert into Chargebacks (trans_id, trans_date) values ('101', '2019-06-30');
insert into Chargebacks (trans_id, trans_date) values ('105', '2019-09-18');

需求

编写一个 SQL 查询,以查找每个月和每个国家/地区的信息:已批准交易的数量及其总金额、退单的数量及其总金额。

输入

select * from transactions;
select * from chargebacks;

在这里插入图片描述
在这里插入图片描述

输出

1.每个月和每个国家/地区的已批准交易的数量

select id,country,state_enum ,amount,to_char(trans_date,'YYYY-MM')  as month,0 as tag
from Transactions
where state_enum  = 'approved';

2.每个月和每个国家/地区的退单的数量

select id,country,state_enum ,amount,to_char(c.trans_date,'YYYY-MM') as month, 1 as tag
from transactions t , chargebacks c 
where  t.id =c.trans_id ;

3.拼接上方两个查询结果,并计算所求

with t1 as(
	select id,country,state_enum ,amount,to_char(trans_date,'YYYY-MM')  as month,0 as tag
	from Transactions
	where state_enum  = 'approved'
	union all 
	select id,country,state_enum ,amount,to_char(c.trans_date,'YYYY-MM') as month, 1 as tag
	from transactions t , chargebacks c 
	where  t.id =c.trans_id
)
select month,country,
	--在 PostgreSQL 中,不支持在聚合函数的参数中直接使用 IF 函数
	count(case when state_enum='approved' and tag=0 then 1 else null end) as approved_count,
	sum(case when state_enum='approved' and tag=0 then amount else 0 end) as approved_amount,
	count(case when tag=1 then 1 else null end) as chargeback_count,
	sum(case when tag=1 then amount else 0 end) as chargeback_amount
from t1
group by month,country
order by month;

在这里插入图片描述

相关推荐

  1. LeetCode】1193. 每月交易 I

    2023-12-05 21:46:04       36 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-05 21:46:04       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-05 21:46:04       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-05 21:46:04       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-05 21:46:04       18 阅读

热门阅读

  1. 逻辑回归 正则化

    2023-12-05 21:46:04       37 阅读
  2. 弱引用能指针 weak_ptr

    2023-12-05 21:46:04       34 阅读
  3. 第八章 django

    2023-12-05 21:46:04       38 阅读
  4. 程序员必备注释模板---佛祖保佑

    2023-12-05 21:46:04       41 阅读
  5. 配置服务器免密登录

    2023-12-05 21:46:04       38 阅读
  6. 计算机网络入门

    2023-12-05 21:46:04       33 阅读
  7. FastDFS部署

    2023-12-05 21:46:04       43 阅读