前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode MySQL 1205. 每月交易II(union all)*

LeetCode MySQL 1205. 每月交易II(union all)*

作者头像
Michael阿明
发布2021-02-19 10:33:09
9480
发布2021-02-19 10:33:09
举报
文章被收录于专栏:Michael阿明学习之路

文章目录

1. 题目

Transactions 记录表

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

Chargebacks 表

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

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

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

查询结果格式如下所示:

代码语言:javascript
复制
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. 解题

代码语言:javascript
复制
# 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_amount
    from 
    (
        select distinct country, date_format(trans_date, '%Y-%m') month
        from Transactions
        union
        select distinct country, date_format(ch.trans_date, '%Y-%m') month
        from Chargebacks ch left join Transactions tr
        on ch.trans_id = tr.id
            
    ) t
    left join
    (
        select date_format(trans_date, '%Y-%m') month, 
            country, 
            count(*) approved_count,
            ifnull(sum(amount),0) approved_amount
        from Transactions
        where state='approved'
        group by month, country
    ) t1
    on t.month = t1.month and t.country = t1.country
    left join 
    (
        select date_format(ch.trans_date, '%Y-%m') month,
        country,
        count(*) chargeback_count,
        ifnull(sum(amount),0) chargeback_amount
        from Chargebacks ch left join Transactions tr
        on ch.trans_id = tr.id
        group by month, country
    ) t2
    on t.month = t2.month and t.country = t2.country
    group by month, country
) tmp
where tmp.approved_count != 0 or tmp.chargeback_count != 0

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

代码语言:javascript
复制
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 transactions
    where state = 'approved'
        union all
    select id, country, 'chargeback' state, amount, c.trans_date
    from chargebacks c left join transactions t 
    on c.trans_id = t.id
) a
group by month,country
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/07/29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 1. 题目
  • 2. 解题
相关产品与服务
云数据库 MySQL
腾讯云数据库 MySQL(TencentDB for MySQL)为用户提供安全可靠,性能卓越、易于维护的企业级云数据库服务。其具备6大企业级特性,包括企业级定制内核、企业级高可用、企业级高可靠、企业级安全、企业级扩展以及企业级智能运维。通过使用腾讯云数据库 MySQL,可实现分钟级别的数据库部署、弹性扩展以及全自动化的运维管理,不仅经济实惠,而且稳定可靠,易于运维。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档