sql20(Leetcode1193每月交易1)

代码:

date_format 可以把具体日期改为不具体的月份或年

# Write your MySQL query statement below

# month country trans_count approved_count trans_total_amount approved_total_amount

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

你可能感兴趣的:(sql)