每日一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): 对分组后的数据按照规则进行累计

相关推荐
合作小小程序员小小店6 分钟前
桌面开发,超市管理系统开发,基于C#,winform,sql server数据库
开发语言·数据库·sql·microsoft·sqlserver·c#
quweiie13 分钟前
thinkphp8.0链接SQL SERVER2022数据库
数据库·sqlserver·thinkphp
Databend1 小时前
如何打造AI时代的数据基石 | Databend Meetup 上海站
数据库
老华带你飞1 小时前
海产品销售系统|海鲜商城购物|基于SprinBoot+vue的海鲜商城系统(源码+数据库+文档)
java·前端·数据库·vue.js·论文·毕设·海鲜商城购物系统
合作小小程序员小小店2 小时前
桌面开发,在线%超市销售管理%系统,基于vs2022,c#,winform,sql server数据
开发语言·数据库·microsoft·c#
SelectDB2 小时前
字节跳动:Apache Doris + AI 一站式融合数据引擎的探索与实践
数据库·apache
IvorySQL2 小时前
PostgreSQL 18 - 时间约束 (Temporal Constraints)
数据库·postgresql·开源
q***61412 小时前
从MySQL迁移到PostgreSQL的完整指南
数据库·mysql·postgresql
N***73853 小时前
SQL锁机制
java·数据库·sql
小羊在奋斗3 小时前
MySQL表的约束:从基础到核心(附场景+案例)
android·数据库·mysql