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

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

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

相关推荐

每晚夜里独自颤抖:要求太多的没必要理
点赞 评论 收藏
分享
嵐jlu:我是山川🐔里🐔🧱的,阿里系简历全过; 你这简历一看就还是半成品啊,没有荣誉经历奖项什么的吗?
投递阿里巴巴集团等公司10个岗位
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-09 16:15
我应届生,去年10月份开始在这家公司实习,到今年10月份正好一年想(实习+试用期),在想要不要提前9月份就离职,这样好找工作些,但又差一个月满一年,又怕10月份国庆回来离职,容易错过了下半年的金九银十,到年底容易gap到年后
小破站_程序员YT:说这家公司不好吧,你干了快一年 说这家公司好吧,你刚毕业就想跑路说你不懂行情吧,你怕错过金九银十说 你懂行情吧,校招阶段在实习,毕业社招想换工作 哥们,我该怎么劝你留下来呢
应届生,你找到工作了吗
点赞 评论 收藏
分享
评论
7
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务