with t as (select 订单号,优惠券码,商品类型,支付金额 from 订单表 join 品类表 using(商品号) where 优惠券码 = 01 and 商品类型 = 'B') select count(订单号) as 总单数,sum(金额) as 总金额 from (select 订单号,sum(t1.支付金额) as 金额 from t join 订单表 t1 using(订单号) group by 订单号) t2