【LeetCode】1193. 每月交易 I

表:Transactions

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| id            | int     |
| country       | varchar |
| state         | enum    |
| amount        | int     |
| trans_date    | date    |
+---------------+---------+
id 是这个表的主键。
该表包含有关传入事务的信息。
state 列类型为 ["approved", "declined"] 之一

编写一个 sql 查询来查找每个月和每个国家/地区的事务数及其总金额、已批准的事务数及其总金额。

以 任意顺序 返回结果表。

查询结果格式如下所示。

 
输入:
Transactions table:
+------+---------+----------+--------+------------+
| id   | country | state    | amount | trans_date |
+------+---------+----------+--------+------------+
| 121  | US      | approved | 1000   | 2018-12-18 |
| 122  | US      | declined | 2000   | 2018-12-19 |
| 123  | US      | approved | 2000   | 2019-01-01 |
| 124  | DE      | approved | 2000   | 2019-01-07 |
+------+---------+----------+--------+------------+
输出:
+----------+---------+-------------+----------------+--------------------+-----------------------+
| month    | country | trans_count | approved_count | trans_total_amount | approved_total_amount |
+----------+---------+-------------+----------------+--------------------+-----------------------+
| 2018-12  | US      | 2           | 1              | 3000               | 1000                  |
| 2019-01  | US      | 1           | 1              | 2000               | 2000                  |
| 2019-01  | DE      | 1           | 1              | 2000               | 2000                  |
+----------+---------+-------------+----------------+--------------------+-----------------------+
with cte1 as(
  select
  Transactions.*,
  DATE_FORMAT(trans_date,'%Y-%m') as 'month'
  from
  Transactions
),
cte2 as(
  select cte1.month,country,
  ifnull(count(1),0) as trans_count,
  ifnull(sum(amount),0) as trans_total_amount
  from cte1
  group by cte1.month,country
),
cte3 as(
  select cte1.month,country,
  ifnull(count(1),0) as approved_count,
  ifnull(sum(amount),0) as approved_total_amount
  from cte1
  where state='approved'
  group by cte1.month,country
)

select cte2.month,cte2.country,
trans_count,
ifnull(approved_count,0) as approved_count,
trans_total_amount,
ifnull(approved_total_amount,0) as approved_total_amount
from cte2 left join cte3
on cte2.month=cte3.month and cte2.country=cte3.country

相关推荐

  1. LeetCode1193. 每月交易 I

    2024-01-06 10:32:03       38 阅读
  2. LeetCode 1193, 45, 48

    2024-01-06 10:32:03       10 阅读
  3. LeetCode每日一题[C++]-1793.好子数组的最大分数

    2024-01-06 10:32:03       18 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-06 10:32:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-06 10:32:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-06 10:32:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-06 10:32:03       20 阅读

热门阅读

  1. LeetCode第102题 - 二叉树的层序遍历

    2024-01-06 10:32:03       40 阅读
  2. 【Android】使用android studio查看内置数据库信息

    2024-01-06 10:32:03       40 阅读
  3. 网络安全协议:保护数据和信息的关键

    2024-01-06 10:32:03       46 阅读
  4. 基于等保合规和滑动标尺模型的云安全建设方法

    2024-01-06 10:32:03       43 阅读
  5. C#的StringBuilder方法

    2024-01-06 10:32:03       33 阅读
  6. 【基础八股文】html css js

    2024-01-06 10:32:03       35 阅读
  7. TCP发送和接受数据

    2024-01-06 10:32:03       38 阅读
  8. ajax/axios/fetch区别及webSocket通信原理

    2024-01-06 10:32:03       39 阅读
  9. 二叉树part04 算法

    2024-01-06 10:32:03       37 阅读
  10. 编写代码中常见问题汇总

    2024-01-06 10:32:03       37 阅读