临时表t 用于记录所有售出商品进价、销售价、数量、状态
再用店铺利润 union 商品利润
with t as(
select t1.*, t3.in_price
from tb_order_detail t1
left join tb_order_overall t2 on t1.order_id = t2.order_id
left join tb_product_info t3 on t3.product_id = t1.product_id
where t2.status = 1
and t2.event_time>='2021-10-01'
and t3.shop_id = 901
)
select '店铺汇总' as product_id
, concat(round((1-sum(t.in_price*t.cnt) / sum(t.price*t.cnt))*100,1),'%') as profit_rate
from t
union
select * from
(
select t.product_id
,concat(round((1-sum(t.in_price*t.cnt) / sum(t.price*t.cnt))*100,1),'%') as profit_rate
from t
group by t.product_id
having (1-sum(t.in_price*t.cnt) / sum(t.price*t.cnt)) > 0.249
order by t.product_id
)t2
;

京公网安备 11010502036488号