LeetCode 1205 每月交易2(PostgreSQL)

数据准备

sql 复制代码
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 查询,以查找每个月和每个国家/地区的信息:已批准交易的数量及其总金额、退单的数量及其总金额。

输入

sql 复制代码
select * from transactions;
select * from chargebacks;


输出

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

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

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

sql 复制代码
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.拼接上方两个查询结果,并计算所求

sql 复制代码
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;
相关推荐
java1234_小锋2 分钟前
Tornado 6.5,全面支持 Python 3.14
数据库·python·tornado
jyOverQ8 分钟前
MySQL 事务到底是什么?ACID 四大特性与隔离级别怎么理解?
数据库·mysql
SelectDB20 分钟前
Doris 直查 Paimon 索引:快手湖上向量检索的共建与落地
大数据·数据库·数据分析
风123456789~20 分钟前
【Oracle专栏】全局 && 本地复合索引 实验
数据库·oracle
1314lay_100722 分钟前
通过Sql Server创建EXCEL文件:master..xp_cmdshell
数据库·excel
zzzll11111 小时前
用 DeepSeek Harness 手搓一个自己的 Agent
数据库
名字还没想好☜1 小时前
Spring @EventListener 事件驱动解耦实战:同步转异步、事务绑定与顺序控制
java·数据库·后端·python·spring
del1 小时前
第十周技术博客
数据库
1314lay_10072 小时前
C#调用Sql Server存储过程,并且使用传入表结构
数据库·经验分享·笔记·sqlserver·c#
anxiao_m2 小时前
2026大文件传输软件推荐,从速度安全多维度客观测评
数据库·文件传输·传输