文章目录
问题

案例

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