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

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

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

  • 两个方案
  • 第一:先计算店铺整体毛利率,再计算各商品毛利率,通过union结合。这个可以结合其他结果看下
  • 第二:with rollup可以计算 group by的总计数据,因此可以通过这个函数得到店铺的值。这样得到的product_id是null,所以需要在 having的时候加上 or product_id is null.然后通过coalesce 或者 ifnull函数把null修改为“店铺汇总”。整体如下:
select 
 coalesce(a.product_id,'店铺汇总') product_id,
 concat(round((1-sum(in_price*cnt)/sum(price*cnt))*100,1),'%') profit_rate
from tb_product_info a join tb_order_overall b join tb_order_detail c
on a.product_id = c.product_id and b.order_id = c.order_id
where a.shop_id = '901' and date_format(b.event_time,'%Y%m') >= '202110'
group by a.product_id 
with rollup
having 1-sum(in_price*cnt)/sum(price*cnt) > 0.249 or product_id is null
order by a.product_id
全部评论

相关推荐

站队站对牛:进度也算很慢的了
点赞 评论 收藏
分享
LuminousZJ:不行,最后还是要看学信网的,这点不能伪装,也骗不过人家,得不偿失
点赞 评论 收藏
分享
评论
3
2
分享

创作者周榜

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