1、明确3张表的含义: ①tb_order_overall:用户订单信息表 as t1 ②tb_order_detail:用户订单中商品及其售价表 as t2(t1.order_id = t2.order_id) ③tb_product_info:用户订单中商品及其进价表 as t3(t2.product_id = t3.product_id) 2、明确需求: ①店铺汇总的毛利率【店铺总毛利率 = (1-总进价成本/总销售收入)*100% = (1-sum(t3.in_price*t2.cnt)/sum(t2.price*t2.cnt))】 ②毛利率大于24.9%的product_id及其毛利率【各个商品的毛利率 = (1-进价/平均单件售价)*100% = (1-sum(t3.in_price*t2.cnt)/sum(t2.price*t2.cnt))】 3、具体操作 step1:分别按照需求,创建两张表 Ⅰ、 select '店铺汇总' as product_id,【由于第1个字段并未进行分组,因此后续的聚合函数就是对全部店铺商品进行的汇总计算】 concat(round((1-sum(t3.in_price*t2.cnt)/sum(t2.price*t2.cnt))*100,1),'%') as profit_rate from tb_order_overall as t1 left join tb_order_detail as t2 on t1.order_id = t2.order_id left join tb_product_info as t3 on t2.product_id = t3.product_id where date_format(event_time,'%Y%m') >= '202110'【event_time VS release_time:客户下单时间√ 商品上架时间×】 and status = 1【用户在付款后并无其它操作】 and t1.shop_id = 901【店铺901】 Ⅱ、 select t2.product_id as product_id, concat(round((1-sum(t1.in_price * t2.cnt)/sum(t2.price * t2.cnt))*100,1),'%') as profit_rate from tb_order_overall as t1 left join tb_order_detail as t2 on t1.order_id = t2.order_id left join tb_product_info as t3 on t2.product_id = t3.product_id where date_format(event_time,'%Y%m') >= '202110'【event_time VS release_time:客户下单时间√ 商品上架时间×】 and status = 1【用户在付款后并无其它操作】 and t1.shop_id = 901【店铺901】 group by 1 having replace(profit_rate,'%','') > 24.9【商品毛利率大于24.9%的商品信息】 step2:将两张表按照需求关联起来 xxx union all xxxx