题解 | #某店铺的各商品毛利率及店铺整体毛利率#
某店铺的各商品毛利率及店铺整体毛利率
https://www.nowcoder.com/practice/65de67f666414c0e8f9a34c08d4a8ba6
SELECT '店铺汇总' AS product_id, CONCAT(ROUND((1 - SUM(in_price*cnt) / SUM(price*cnt))*100, 1), '%') AS profit_rate FROM tb_order_detail t3 JOIN tb_order_overall t2 ON t3.order_id = t2.order_id JOIN tb_product_info t1 ON t3.product_id = t1.product_id WHERE shop_id = 901 AND event_time > '2021-10-01' UNION SELECT t3.product_id, CONCAT(ROUND((1 - SUM(in_price*cnt) / SUM(price*cnt))*100, 1), '%') AS profit_rate FROM tb_order_detail t3 JOIN tb_order_overall t2 ON t3.order_id = t2.order_id JOIN tb_product_info t1 ON t3.product_id = t1.product_id WHERE shop_id = 901 AND event_time > '2021-10-01' GROUP BY product_id HAVING ROUND((1 - SUM(in_price*cnt) / SUM(price*cnt))*100, 1) > 24.9
分别查询得到店铺汇总的毛利率和各商品的毛利率,再使用UNION函数将其合并。