文章目录

  • 1. 题目
  • 2. 解题

1. 题目

Transactions 记录表

+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| id             | int     |
| country        | varchar |
| state          | enum    |
| amount         | int     |
| trans_date     | date    |
+----------------+---------+
id 是这个表的主键。
该表包含有关传入事务的信息。
状态列是类型为 [approved(已批准)、declined(已拒绝)] 的枚举。

Chargebacks 表

+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| trans_id       | int     |
| charge_date    | date    |
+----------------+---------+
退单包含有关放置在事务表中的某些事务的传入退单的基本信息。
trans_id 是 transactions 表的 id 列的外键。
每项退单都对应于之前进行的交易,即使未经批准。

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

注意:在您的查询中,给定月份和国家,忽略所有为零的行

查询结果格式如下所示:

Transactions 表:
+------+---------+----------+--------+------------+
| id   | country | state    | amount | trans_date |
+------+---------+----------+--------+------------+
| 101  | US      | approved | 1000   | 2019-05-18 |
| 102  | US      | declined | 2000   | 2019-05-19 |
| 103  | US      | approved | 3000   | 2019-06-10 |
| 104  | US      | declined | 4000   | 2019-06-13 |
| 105  | US      | approved | 5000   | 2019-06-15 |
+------+---------+----------+--------+------------+Chargebacks 表:
+------------+------------+
| trans_id   | trans_date |
+------------+------------+
| 102        | 2019-05-29 |
| 101        | 2019-06-30 |
| 105        | 2019-09-18 |
+------------+------------+Result 表:
+----------+---------+----------------+-----------------+-------------------+--------------------+
| month    | country | approved_count | approved_amount | chargeback_count  | chargeback_amount  |
+----------+---------+----------------+-----------------+-------------------+--------------------+
| 2019-05  | US      | 1              | 1000            | 1                 | 2000               |
| 2019-06  | US      | 2              | 8000            | 1                 | 1000               |
| 2019-09  | US      | 0              | 0               | 1                 | 5000               |
+----------+---------+----------------+-----------------+-------------------+--------------------+

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/monthly-transactions-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 解题

# Write your MySQL query statement below
select *
from
(select t.month, t.country, ifnull(sum(t1.approved_count),0) approved_count, ifnull(sum(t1.approved_amount),0) approved_amount,ifnull(sum(t2.chargeback_count),0) chargeback_count, ifnull(sum(t2.chargeback_amount),0) chargeback_amountfrom (select distinct country, date_format(trans_date, '%Y-%m') monthfrom Transactionsunionselect distinct country, date_format(ch.trans_date, '%Y-%m') monthfrom Chargebacks ch left join Transactions tron ch.trans_id = tr.id) tleft join(select date_format(trans_date, '%Y-%m') month, country, count(*) approved_count,ifnull(sum(amount),0) approved_amountfrom Transactionswhere state='approved'group by month, country) t1on t.month = t1.month and t.country = t1.countryleft join (select date_format(ch.trans_date, '%Y-%m') month,country,count(*) chargeback_count,ifnull(sum(amount),0) chargeback_amountfrom Chargebacks ch left join Transactions tron ch.trans_id = tr.idgroup by month, country) t2on t.month = t2.month and t.country = t2.countrygroup by month, country
) tmp
where tmp.approved_count != 0 or tmp.chargeback_count != 0

or 简单写法,创建一个 chargeback state

select date_format(a.trans_date,'%Y-%m') month,country,sum(state = 'approved') approved_count,sum(if(state = 'approved',amount,0)) approved_amount,sum(state = 'chargeback') chargeback_count,sum(if(state = 'chargeback',amount,0)) chargeback_amount
from
(select * from transactionswhere state = 'approved'union allselect id, country, 'chargeback' state, amount, c.trans_datefrom chargebacks c left join transactions t on c.trans_id = t.id
) a
group by month,country

我的CSDN博客地址 https://michael.blog.csdn.net/

长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!

LeetCode MySQL 1205. 每月交易II(union all)*相关推荐

  1. LeetCode MySQL 1193. 每月交易 I(date_format)

    文章目录 1. 题目 2. 解题 1. 题目 Table: Transactions +---------------+---------+ | Column Name | Type | +----- ...

  2. LeetCode MySQL 602. 好友申请 II :谁有最多的好友(union all)

    文章目录 1. 题目 2. 解题 1. 题目 在 Facebook 或者 Twitter 这样的社交应用中,人们经常会发好友申请也会收到其他人的好友申请. 表 request_accepted 存储了 ...

  3. LeetCode MySQL 1264. 页面推荐(union)

    文章目录 1. 题目 2. 解题 1. 题目 朋友关系列表: Friendship +---------------+---------+ | Column Name | Type | +------ ...

  4. LeetCode MySQL 1149. 文章浏览 II

    文章目录 1. 题目 2. 解题 1. 题目 Table: Views +---------------+---------+ | Column Name | Type | +------------ ...

  5. LeetCode MySQL 1076. 项目员工II

    文章目录 1. 题目 2. 解题 1. 题目 Table: Project +-------------+---------+ | Column Name | Type | +------------ ...

  6. LeetCode MySQL解题目录

    已完成的 LeetCode MySQL 数据库题目.点击查看我的 LeetCode 算法解题目录. 已解决 123/123 - 简单 54 中等 51 困难 18 前置入门学习 MySQL 基本查询. ...

  7. LeetCode实战:环形链表 II

    背景 为什么你要加入一个技术团队? 如何加入 LSGO 软件技术团队? 我是如何组织"算法刻意练习活动"的? 为什么要求团队的学生们写技术Blog 题目英文 Given a lin ...

  8. LeetCode MySQL 1308. 不同性别每日分数总计(累加/变量/窗口函数)

    文章目录 1. 题目 2. 解题 1. 题目 表: Scores +---------------+---------+ | Column Name | Type | +--------------- ...

  9. MySQL入门 (四) : JOIN 与UNION 查询

    1 使用多个表格 在「world」资料库的「country」表格中,储存世界上所有的国家资料,其中有一个栏位「Capital」用来储存首都资料,不过它只是储存一个编号:另外在「city」表格中,储存世 ...

最新文章

  1. 2018 百越杯 pwn(format WriteUp)
  2. 图解VC#版DirectX开发教程二 - 摄像机
  3. Qt Creator调试C ++示例应用程序
  4. 分区和分片的区别_MySQL分区与分片的差异
  5. 关于Go结构体内存大小的一点小知识
  6. 时间序列研(part8)--ADF检验
  7. ThoughtWorks洞见领域驱动设计思维导图笔记
  8. 查看服务器内存、CPU、网络等占用情况的命令--汇总
  9. LightOJ 1013 LCS+记忆化搜索
  10. error:Assertion failed ((unsigned)i0 (unsigned)size.p[0]) in cv::Mat::at
  11. 亿万富翁Mark Cuban:比特币是像黄金一样的价值存储,更多是一种信仰
  12. Jdk1.6 JUC源码解析(12)-ArrayBlockingQueue
  13. Vue项目中安装axios
  14. 矩阵快速幂(原理+模板)
  15. sspanel v3 配置的完整教程(转)
  16. wps-doc文件输出为pdf文件时目录报错“错误!未定义书签”解决方法
  17. God-Mz公益版秒赞系统源码
  18. 熵值法 java_Java实现熵值法确定权重
  19. 推荐系统经典算法之——MF(矩阵分解)
  20. QTcreater学习笔记

热门文章

  1. global全局变量
  2. Python开发中收集的一些常用功能Demo
  3. AIML元素详细说明
  4. 如何在VMWare的Ubuntu虚拟机中设置共享文件夹
  5. 面试题:根据Unix时间戳计算时间
  6. android 6.0 ios9谁快,没安卓6.0流畅?iOS 9突飞猛进终于不卡
  7. ARM的7种工作模式、37个通用寄存器、CPSR程序状态寄存器
  8. 使用OpenXml打开word文档中嵌入的另一个文档
  9. RocketMQ实战(一)
  10. 互联网产品 从设计到运营 这中间提高须要关注的站点