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;
相关推荐
君不见,青丝成雪27 分钟前
Hadoop技术栈(四)HIVE常用函数汇总
大数据·数据库·数据仓库·hive·sql
不羁。。6 小时前
【撸靶笔记】第七关:GET - Dump into outfile - String
数据库·笔记·oracle
yangchanghua1117 小时前
pgsql 如何查询今天范围内的数据(当天0点0分0秒 - 当天23点59分59秒....)
数据库·pgsql
larance7 小时前
SQLAlchemy 的异步操作来批量保存对象列表
数据库·python
python_chai7 小时前
从数据汇总到高级分析,SQL 查询进阶实战(下篇)—— 分组、子查询与窗口函数全攻略
数据库·sql·mysql
在努力的前端小白8 小时前
Spring Boot 敏感词过滤组件实现:基于DFA算法的高效敏感词检测与替换
java·数据库·spring boot·文本处理·敏感词过滤·dfa算法·组件开发
未来之窗软件服务8 小时前
自建知识库,向量数据库 (九)之 量化前奏分词服务——仙盟创梦IDE
数据库·仙盟创梦ide·东方仙盟·自建ai·ai分词
冒泡的肥皂11 小时前
MVCC初学demo(一
数据库·后端·mysql
圣保罗的大教堂11 小时前
leetcode 2348. 全 0 子数组的数目 中等
leetcode
.Shu.12 小时前
Redis Reactor 模型详解【基本架构、事件循环机制、结合源码详细追踪读写请求从客户端连接到命令执行的完整流程】
数据库·redis·架构