LeetCode 1205 每月交易2(PostgreSQL)

news/2024/7/9 22:57:45 标签: leetcode, postgresql, 数据库, sql

数据准备

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;

在这里插入图片描述


http://www.niftyadmin.cn/n/5231870.html

相关文章

MySQL:找回root密码

一、情景描述 我们在日常学习中,经常会忘记自己的虚拟机中MySQL的root密码。 这个时候,我们要想办法重置root密码,从而,解决root登陆问题。 二、解决办法 1、修改my.cnf配置文件并重启MySQL 通过修改配置文件,来跳…

iris+vue上传到本地存储【go/iris】

iris部分 //main.go package mainimport ("fmt""io""net/http""os" )//上传视频文件部分 func uploadHandler_video(w http.ResponseWriter, r *http.Request) {// 解析上传的文件err : r.ParseMultipartForm(10 << 20) // 设置…

SpringBoot进行消息推送的的几种方式

Spring Boot进行消息推送的几种方式包括&#xff1a; WebSocket SockJS STOMP Server-Sent Events (SSE) Push Notifications 以下是WebSocket的案例代码&#xff1a; 添加WebSocket依赖 <dependency><groupId>org.springframework.boot</groupId>&l…

2023年12月2日历史上的今天大事件早读

823年12月2日 《门罗宣言》发表 1908年12月2日 末代皇帝溥仪登基 1919年12月2日 平江开展驱除湘督张敬尧运动 1929年12月2日 北平周口店发现中国猿人头盖骨 1941年12月2日 美裔华人物理学家朱经武出生 1949年12月2日 中央决定发行人民胜利公债 1952年12月2日 拿破仑三世成…

Kubernetes 使用插件扩展 kubectl

例子演示 编写 kubectl-foo &#xff0c;拷贝至 /usr/local/bin/ #!/bin/bash# 可选的参数处理 if [[ "$1" "version" ]] thenecho "1.0.0"exit 0 fi# 可选的参数处理 if [[ "$1" "config" ]] thenecho $KUBECONFIGexit…

vue v-permission权限指令

控制页面及按钮的显示隐藏 src/directive/permission/index.js import permission from ./permissionconst install function(Vue) {Vue.directive(permission, permission) }if (window.Vue) {window[permission] permissionVue.use(install); // eslint-disable-line }per…

SCA技术进阶系列(四):DSDX SBOM供应链安全应用实践

一、SBOM的发展趋势 数字时代&#xff0c;软件已经成为维持生产生活正常运行的必备要素之一。随着容器、中间件、微服务、 DevOps等技术理念的演进&#xff0c;软件行业快速发展&#xff0c;但同时带来软件设计开发复杂度不断提升&#xff0c;软件供应链愈发复杂&#xff0c;软…

SpringBootWeb案例_02

Web后端开发_05 SpringBootWeb案例_02 1.新增员工 1.1需求 在新增用户时&#xff0c;我们需要保存用户的基本信息&#xff0c;并且还需要上传的员工的图片&#xff0c;目前我们先完成第一步操作&#xff0c;保存用户的基本信息。 1.2 接口文档 基本信息 请求路径&#xff…