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

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

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

首先,利用表连接,将店铺商品信息与用户的订单购买信息结合起来,合并成宽表,提取出我们所需要计算的成本总额,收入总额等,找出所需要的信息作为临时表:

1.找出901号店铺的产品信息,作为左表

2.使用left join 连接产品与订单表中相关的金额与单量数据,就过滤掉了其他店铺的货物信息

with c as 
(
    select product_id
        ,sum(price*cnt) as sum_sold
        ,sum(in_price*cnt) as sum_in
    from
        (
        select a.product_id
              ,a.in_price
              ,a.quantity
              ,t.order_id
              ,t.price
              ,t.cnt
              ,o.total_amount
              ,o.total_cnt
        from
        (select* from tb_product_info where shop_id='901')a #注意限定店铺号
        left join tb_order_detail t 
        on a.product_id = t.product_id
        left join tb_order_overall o on t.order_id = o.order_id
        where date_format(event_time,'%Y-%m') >='2021-10' and o.status = 1
        )b
    group by product_id
    )

接着,将需要计算的所有参数进行计算

1.注意保留小数使用round

2.最终表现用%链接,所以需要计算得数*100后,使用concat与其拼接

select '店铺汇总'as product_id
      ,concat(round((1-sum(sum_in)/sum(sum_sold))*100,1),'%') as profit_rate
from c
    union
select product_id
      ,concat(round((1-sum_in/sum_sold)*100,1),'%') as profit_rate
from c  
where round((1-sum_in/sum_sold)*100,1) > 24.9

最后的脚本表示为:

with c as 
(
    select product_id
        ,sum(price*cnt) as sum_sold
        ,sum(in_price*cnt) as sum_in
    from
        (
        select a.product_id
              ,a.in_price
              ,a.quantity
              ,t.order_id
              ,t.price
              ,t.cnt
              ,o.total_amount
              ,o.total_cnt
        from
        (select* from tb_product_info where shop_id='901')a #注意限定店铺号
        left join tb_order_detail t 
        on a.product_id = t.product_id
        left join tb_order_overall o on t.order_id = o.order_id
        where date_format(event_time,'%Y-%m') >='2021-10' and o.status = 1
        )b
    group by product_id
    )
    select '店铺汇总'as product_id
      ,concat(round((1-sum(sum_in)/sum(sum_sold))*100,1),'%') as profit_rate
from c
    union
select product_id
      ,concat(round((1-sum_in/sum_sold)*100,1),'%') as profit_rate
from c  
where round((1-sum_in/sum_sold)*100,1) > 24.9
数据库刷题题解 文章被收录于专栏

数据分析数据库题目练习题解

全部评论

相关推荐

拒绝996的悲伤蛙:此贴终结|给路过的牛友分享一下心得👇 实习的时候不要光埋头干活,身边的大佬同事才是真·宝藏人脉!大胆请教他们工作以及职场上的问题以我的经历,我的带教有十几年工作经验,做过运维、后端开发、web测试,现在是高级软测,是行走的避坑指南 我之前纠结要不要学Web测试简历,被他一句话点醒:Web发展成熟,岗位需求在缩,AI对互联网的冲击可能以后架构+开发+测试一人包揽。现在用户更多用的是移动端APP/小程序,相比之下天天守着电脑刷网页的人基数小。 这里我的纠结得到反馈,于是我又把简历发给带教,获得了一对一的简历指导。 感兴趣的可以看看: 1.教育背景:本科→本科(全日制) 2.实习经历:总体问题不大,但第2点要稍作修改,可以写但做功课,如风机、水箱……可能会问用哪个供应商的?使用寿命、型号、电压电流、多少秒会触发逻辑? 3.项目经历(坑太多,大型翻车现场): - 项目名越直白越好,让人一眼就知道你干了啥。 -用的什么语言设计核心接口,异步执行做功课,涉及线程问题,被问可回答n个功能是如何错开异步执行的 - “验证任务消费……阻塞丢包”“高负载稳定性”这种词,没三五年开发功底不要写,不然面试时被问线程、数量级、CPU占用,内存带宽等影响性能的直接原地社死。 -做功课 -做功课,测了哪些模块,如何设计,接口流量抓包,token,变量…… -做功课,要熟悉网络协议…… 带教之前做互联网开发的时候面试过很多人,总的来说不要为了显得项目高大上过渡包装,写了就要做好拷打的准备
听劝,我这个简历该怎么改...
点赞 评论 收藏
分享
03-07 17:51
已编辑
南华大学 后端工程师
asdasdasda...:也不知道是不是真的被逼呢,也有可能女方有很多东西瞒着男方,这种东西男方什么情况都不知道全靠女方说,很难评的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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