牛客sql面试题 SQL15 某店铺的各商品毛利率及店铺整体毛利率

牛客sql面试题 SQL15 某店铺的各商品毛利率及店铺整体毛利率

  • 问题:请计算2021年10月以来店铺901中商品 毛利率大于24.9的商品信息及店铺整体毛利率。
  • 商品毛利率=(1-进价/平均单件售价)*100%
    -店铺毛利率=(1-总进价成本/总销售收入)*100%

在这里插入图片描述

  • 思路,先计算店铺汇总 再纵向连接商品毛利率。
    1.店铺毛利率=(1-总进价成本/总销售收入)*100%。
    – 观察三表可知
#店铺901,2021年10月以来
  -- 总进价价成本= sum(in_price*cnt) as 总进价
  -- 总销售收入  sum( price*cnt ) as 总销售
	select shop_id,
		price*cnt as 总销售,
		in_price*cnt 总进货,event_time
	from tb_order_detail t3 left join tb_product_info t1
		on t1.product_id=t3.product_id join tb_order_overall t2 on t2.order_id=t3.order_id
		where shop_id='901' and date(event_time)>='2021-10-01'

在这里插入图片描述

  • 注意点:
  • 错误点:date(event_time) >=2021-10-01 #要打引号。
select '店铺汇总' as product_id,
	 concat(round((1-sum(总进货)/sum(总销售))*100,1),'%') profit_rate
from(
	select shop_id,
		price*cnt as 总销售,
		in_price*cnt 总进货,event_time
	from tb_order_detail t3 left join tb_product_info t1
		on t1.product_id=t3.product_id join tb_order_overall t2 on t2.order_id=t3.order_id
		where shop_id='901' and date(event_time)>='2021-10-01'
	) t;

在这里插入图片描述

-计算商品毛利率 公式同上

select t.product_id as product_id,
    concat(round((1-sum(in_price*cnt)/sum(price*cnt))*100,1),'%') as profit_rate
	from tb_product_info t join tb_order_overall t1 join tb_order_detail t2 on t.product_id = t2.product_id and t1.order_id = t2.order_id
	where shop_id=901  and date(event_time)>='20211001'
	group by t.product_id
	having profit_rate>0.249
	order by product_id
  • 最后将两表连接
select '店铺汇总' as product_id,
	concat(round((1-sum(in_price*cnt)/sum(price*cnt))*100,1),'%') as profit_rate
from tb_product_info t join tb_order_overall t1 join tb_order_detail t2 on t.product_id = t2.product_id and t1.order_id = t2.order_id
where shop_id=901  and date(event_time)>='20211001'
union 
	(select t.product_id as product_id,
    concat(round((1-sum(in_price*cnt)/sum(price*cnt))*100,1),'%') as profit_rate
	from tb_product_info t join tb_order_overall t1 join tb_order_detail t2 on t.product_id = t2.product_id and t1.order_id = t2.order_id
	where shop_id=901  and date(event_time)>='20211001'
	group by t.product_id
	having profit_rate>0.249
	order by product_id);

在这里插入图片描述

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
2023全牛客史上最全MySQL大厂常问面试题合集,是一份总结了许多MySQL常见面试问题的资料。在MySQL作为关系型数据库中的重要一员,业界使用率极高,成为了大厂面试的必考点之一。对于面试者来说,掌握MySQL相关知识无疑是非常重要的。 面对这样一份面试题合集,我们需要掌握MySQL的基本架构、性能优化、存储引擎等方面的知识。首先,我们需要了解MySQL的基本架构,即MySQL架构的三层结构,包括连接处理层、查询处理层和存储引擎层。此外,对于查询语句的优化,我们需要了解索引的使用、查询语句的执行流程等概念,并且掌握MySQL自带的调优工具。 在MySQL性能优化方面,我们可以从硬件、操作系统、MySQL本身以及SQL语句的角度入手。例如,可以从MySQL参数配置、SQL执行计划、SQL调优等方面解决性能问题。同时,由于存储引擎对于MySQL的性能非常关键,我们也需要了解InnoDB和MyISAM这两种主流存储引擎的特点与优缺点,及其在实际应用中的使用情况。 总之,掌握MySQL的基本架构、性能优化、存储引擎等知识是应聘MySQL岗位时不可或缺的必备技能。面对这份全牛客史上最全MySQL大厂常问面试题合集,需要认真学习、反复练习,并对自己的掌握情况进行不断的总结与提高。只有做到这些,才能在面试中展现出自己的实力,获得满意的工作机会。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值