题解 | #某店铺的各商品毛利率及店铺整体毛利率#
某店铺的各商品毛利率及店铺整体毛利率
http://www.nowcoder.com/practice/65de67f666414c0e8f9a34c08d4a8ba6
-- 商品毛利率=(1-进价/平均单件售价)*100%; -- 店铺毛利率=(1-总进价成本/总销售收入)*100%。
解题思路
- 首先联合三张表,用left join或join均可
- 筛选出来条件:901,时间大于2021-10(这里注意!我之前一直写的=2021-10,才发现题目是10月以来!),状态=1我觉得也挺重要,如果有退货退款,那就不能算进去(我回去把 status=1删除了,发现也可以通过,特来更新)
- 按照product_id分类
- 累加with rollup计算店铺汇总
- 筛选出来profit_rate>24.9或者product_id is Null(万一店铺汇总毛利率低于24.9,要避免这个情况,因为店铺汇总不管是多少都要输出)
- product_id排序
select
ifnull(product_id,'店铺汇总') as product_id,
concat(round((1-(sum(cnt*in_price))/(sum(cnt*price)))*100,1),'%') as profit_rate
from tb_order_detail tod
left join tb_product_info tpi
using(product_id)
left join tb_order_overall too
using(order_id)
where shop_id=901 and date_format(event_time,'%Y-%m')>='2021-10' and status=1
group by tod.product_id
with rollup #累加
having profit_rate>24.9 or** product_id is Null**#这一句很重要,参考了别人的才做出来
order by product_id