题目描述:
要求写一个SQL查询,对于所有产品,返回每个产品的产品名称,以及全部发票累计的总应缴款项、总已支付金额、总已取消金额、总已退款金额。
查询结果按 product_name 排序
示例:
方法1:
主要思路:解题链接汇总
select p.name as name,
ifnull(sum(i.rest),0) as rest,
ifnull(sum(i.paid),0) as paid,
ifnull(sum(i.canceled),0) as canceled,
ifnull(sum(i.refunded),0) as refunded
from Product as p left join Invoice as i on p.product_id=i.product_id
group by p.name
order by p.name