题解 | #某店铺的各商品毛利率及店铺整体毛利率#
某店铺的各商品毛利率及店铺整体毛利率
https://www.nowcoder.com/practice/65de67f666414c0e8f9a34c08d4a8ba6
-- 思路:
-- 1.先统计出2021年10月以来店铺901已付款的商品信息
-- 2.分别统计商品的毛利率和店铺汇总的毛利率,然后再union all汇总
-- 指标定义:
-- 商品毛利率=(1-进价/平均单件售价)*100%
-- 店铺毛利率=(1-总进价成本/总销售收入)*100%
-- 思路:
-- 1.先统计出2021年10月以来店铺901已付款的商品信息
-- 2.分别统计商品的毛利率和店铺汇总的毛利率,然后再union all汇总
-- 指标定义:
-- 商品毛利率=(1-进价/平均单件售价)*100%
-- 店铺毛利率=(1-总进价成本/总销售收入)*100%
with t as(
-- 1.先统计出2021年10月以来店铺901已付款的商品信息
select
p.product_id,
p.in_price,
d.price,
d.cnt
from tb_order_overall o
left join tb_order_detail d on o.order_id = d.order_id
left join tb_product_info p on d.product_id = p.product_id
where o.status = 1
and date_format(o.event_time, '%Y-%m-%d') >= '2021-10-01'
and p.shop_id = 901
)
-- 2.分别统计商品的毛利率和店铺汇总的毛利率,然后再union all汇总
-- 店铺汇总 毛利率(保留一位小数)
select
'店铺汇总' as product_id,
concat(round((1-sum(in_price * cnt)/ sum(price*cnt))*100, 1), '%') as profit_rate
from t
union all
-- 商品毛利率大于24.9%的商品信息
select *
from
(select
product_id,
concat(round((1-sum(in_price*cnt)/sum(price*cnt))*100, 1), '%') as profit_rate
from t
group by product_id having round((1-sum(in_price*cnt)/sum(price*cnt))*100, 1) > 24.9
order by product_id
) t1
SQL大厂面试题 文章被收录于专栏
牛客网sql大厂面试题题解~


途虎成长空间 159人发布