每日一SQL 【每月交易 I】

文章目录

问题

案例

执行顺序

select,from,where,group by,order by,having, limit执行顺序

使用分组解决

sql 复制代码
select
    date_format(trans_date, '%Y-%m') as month,
    country,
    count(*) as trans_count,
    sum(state = 'approved') as approved_count,
    sum(amount) as trans_total_amount,
    sum(case when state = 'approved' then amount else 0 end) as approved_total_amount
from Transactions
group by date_format(trans_date, '%Y-%m'), country;

解释

date_format: 时间格式化函数

sum(case when...then...else...end): 对分组后的数据按照规则进行累计

相关推荐
麦聪聊数据3 小时前
数据服务化时代:企业数据能力输出的核心路径
数据库
shushangyun_4 小时前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
DARLING Zero two♡4 小时前
【MySQL数据库】数据类型与表约束
数据库·mysql
曹牧4 小时前
Oracle EXPLAIN PLAN
数据库·oracle
BD_Marathon4 小时前
SQL学习指南——视图
数据库·sql
活宝小娜5 小时前
mysql详细安装教程
数据库·mysql·adb
贤时间5 小时前
codex 助力oracle ebs 开发
数据库·oracle
秉承初心5 小时前
PostgreSQL 数据性能瓶颈突破实战
数据库·postgresql·oracle
2601_962072555 小时前
李梦娇常识4600问|题库|打印版
sql·华为od·华为·c#·华为云·.net·harmonyos