题解 | #某店铺的各商品毛利率及店铺整体毛利率#

某店铺的各商品毛利率及店铺整体毛利率

http://www.nowcoder.com/practice/65de67f666414c0e8f9a34c08d4a8ba6

不难,但是步骤繁琐,做题时要注意status,然后就是union和order by 的先后顺序了 在转变成百分比的时候括号特别多,不要少了

 with tmp as  (SELECT id,
       sum(in_price*if(status=2,-cnt,cnt)) as all_in,
       sum(price*if(status=2,-cnt,cnt)) as all_out
 from (
select de.product_id as id,
       in_price,
       status,
       price,
       cnt
from 
tb_order_detail de
left join 
tb_order_overall  ov
on de.order_id=ov.order_id
left join 
tb_product_info info
on de.product_id=info.product_id
where DATE_FORMAT(event_time,"%Y-%m")>="2021-10"
      and shop_id=901) base
                group by id)
      
select "店铺汇总" as product_id,concat(round((1-sum(all_in)/sum(all_out))*100
                            ,1),"%")
from tmp
union (
select id as product_id,concat(round((1-all_in/all_out)*100
                            ,1),"%")
from tmp
where (1-all_in/all_out)>=0.249
order by product_id)

全部评论
如果你考虑了status=2的情况,那应该在with as 的where那里加上status!=0
1 回复 分享
发布于 2022-03-17 08:50

相关推荐

我是小红是我:学校换成中南
点赞 评论 收藏
分享
7 1 评论
分享
牛客网
牛客企业服务