计算商城中2021年每月的GMV

明确题意:

统计GMV大于10w的每月GMV


问题分解:

  • 筛选满足条件的记录:
    • 退款的金额不算(付款的记录还在,已算过一次):where status != 2
    • 2021年的记录:and YEAR(event_time) = 2021
  • 按月份分组:group by DATE_FORMAT(event_time, "%Y-%m")
  • 计算GMV:(sum(total_amount) as GMV
  • 保留整数:ROUND(x, 0)
  • 筛选GMV大于10w的分组:having GMV > 100000

细节问题:

  • 表头重命名:as
  • 按GMV排序:order by GMV;

完整代码:

select DATE_FORMAT(event_time, "%Y-%m") as `month`,
    ROUND(sum(total_amount), 0) as GMV
from tb_order_overall
where status != 2 and YEAR(event_time) = 2021
group by `month`
having GMV > 100000
order by GMV;