当前位置: 首页 > news >正文

SQL-leetcode—1193. 每月交易 I

1193. 每月交易 I

表:Transactions

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

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

以 任意顺序 返回结果表。

查询结果格式如下所示。

示例 1:

输入:
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 |
±---------±--------±------------±---------------±-------------------±----------------------+

题解

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

  • 查找每个月和每个国家/地区的 – group by
  • 事务数及其总金额、已批准的事务数及其总金额 – 有条件的聚合

方法一: date_format + group by + 聚合函数

selectdate_format(trans_date,'%Y-%m') as month,country,count(id) as trans_count,sum(if(state='approved',1,0)) as approved_count,sum(amount) as trans_total_amount,sum(if(state='approved',amount,0)) as approved_total_amount
from Transactions
group by country,date_format(trans_date,'%Y-%m')

方法二:left + group by + 聚合函数

select left(trans_date ,7) month
,country 
,count(state) trans_count 
,count(case when state = 'approved' then '1' else null end) approved_count
,sum(amount )trans_total_amount
,sum(case when state = 'approved' then amount else '0' end)approved_total_amountfrom Transactions a  group by left(trans_date ,7),country 

比较简单就这样吧

http://www.lryc.cn/news/528300.html

相关文章:

  • 【MySQL — 数据库增删改查操作】深入解析MySQL的 Retrieve 检索操作
  • 项目开发实践——基于SpringBoot+Vue3实现的在线考试系统(九)(完结篇)
  • 离散 VS 流程制造,制造业的 “双生花” 如何绽放
  • freeswtch目录下modules.conf各个模块的介绍【freeswitch版本1.6.8】
  • 循序渐进kubernetes-RBAC(Role-Based Access Control)
  • 第3章 基于三电平空间矢量的中点电位平衡策略
  • 基于SpringBoot的阳光幼儿园管理系统
  • Python 数据分析 - Matplotlib 绘图
  • uniapp版本升级
  • Django ORM解决Oracle表多主键的问题
  • 机器学习2 (笔记)(朴素贝叶斯,集成学习,KNN和matlab运用)
  • ubuntu解决普通用户无法进入root
  • Time Constant | RC、RL 和 RLC 电路中的时间常数
  • 数据结构测试题2
  • 在虚拟机里运行frida-server以实现对虚拟机目标软件的监测和修改参数(一)(android Google Api 35高版本版)
  • mysql_store_result的概念和使用案例
  • Linux进程调度与等待:背后的机制与实现
  • 网易云音乐歌名可视化:词云生成与GitHub-Pages部署实践
  • 单片机基础模块学习——DS18B20温度传感器芯片
  • 《网络数据安全管理条例》施行,企业如何推进未成年人个人信息保护(下)
  • 书生大模型实战营3
  • Spring Boot 集成 WebClient 实战教程 实现同步、异步请求处理以及响应式编程、响应式流、响应式Mono
  • C语言深入解析 printf的底层源码实现
  • go 循环处理无限极数据
  • C# Dynamic关键字
  • ReactNative react-devtools 夜神模拟器连调
  • 【教学类-89-02】20250128新年篇02——姓名藏头对联(星火讯飞+Python,五言对联,有横批)
  • 装机爱好者的纯净工具箱
  • 【新春不断更】数据结构与算法之美:二叉树
  • 网站结构优化:加速搜索引擎收录的关键