此题共包含三张表:
表1:tb_order_detail 表2:tb_order_overall 表3:tb_product_info
要解决的问题:
请计算2021年10月以来店铺901中商品毛利率大于24.9%的商品信息及店铺整体毛利率。
解题思路:
- 获取2021年10月,店铺为901的商品信息
- 计算需要用到的商品信息,如毛利率、店铺毛利率中涉及的各项指标
- 店铺毛利率和商品毛利率需要分两张表来计算,然后union到一起,题目要求要先输出店铺毛利率,所以第一张表先做这个
- 计算商品毛利率,且商品毛利率要大于24.9,计算完毕后按毛利率升序排列
select '店铺汇总' as pproduct_id,
concat(round((1-sum(in_price*cnt)/sum(price*cnt))*100,1),'%') as profit_rate
from tb_order_detail tod
join tb_order_overall too
on tod.order_id = too.order_id
join tb_product_info tpi
on tpi.product_id = tod.product_id
where shop_id=901 and date(event_time)>='20211001'
union
select *
from
(select tod.product_id as pproduct_id,
concat(round((1-sum(in_price*cnt)/sum(price*cnt))*100,1),'%') as profit_rate
from tb_order_detail tod
join tb_order_overall too
on tod.order_id = too.order_id
join tb_product_info tpi
on tpi.product_id = tod.product_id
where shop_id=901 and date(event_time)>='20211001'
group by tod.product_id
having 1-sum(in_price*cnt)/sum(price*cnt)>0.249
order by pproduct_id) t