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

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

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

with
    t1 as (
        select
            ti.product_id,
            in_price,
            price,
            cnt
        from
            tb_product_info ti
            left join tb_order_detail td on ti.product_id = td.product_id
            left join tb_order_overall tl on td.order_id = tl.order_id
        where
            shop_id = '901'
            and date (event_time) >= '2021-10-01'
    ),
    t2 as (
        select
            product_id,
            sum(cnt) as t_cnt,
            sum(t1.price * t1.cnt) as t_m
        from
            t1
        group by
            product_id
    ),
    t3 as (
        select
            concat (
                round(
                    (1 - sum(in_price * cnt) / sum(price * cnt)) * 100,
                    1
                ),
                '%'
            ) as profit_rate
        from
            t1
    ),
    t4 as (
        select distinct
            t1.product_id,
            concat (
                round((1 - t1.in_price * t_cnt / t_m) * 100, 1),
                '%'
            ) as profit_rate,
            2 as num
        from
            t1
            left join t2 on t1.product_id = t2.product_id
        where
            round((1 - t1.in_price * t_cnt / t_m) * 100, 1) > 24.9
        union all
        select
            '店铺汇总' as product_id,
            profit_rate,
            1 as num
        from
            t3
    )
select
    product_id,
    profit_rate
from
    t4
order by
    num,
    product_id

全部评论

相关推荐

秋招不是要开始了吗,我都打算润了,看大家还在找不敢润了
一条从:因为不是人人都像佬一样有实习像我们这种二本仔秋招没有实习也是白忙活
点赞 评论 收藏
分享
05-07 17:58
门头沟学院 Java
wuwuwuoow:1.简历字体有些怪怪的,用啥写的? 2.Redis 一主二从为什么能解决双写一致性? 3.乐观锁指的是 SQL 层面的库存判断?比如 stock > 0。个人认为这种不算乐观锁,更像是乐观锁的思想,写 SQL 避免不了悲观锁的 4.奖项证书如果不是 ACM,说实话没什么必要写 5.逻辑过期时间为什么能解决缓存击穿问题?逻辑过期指的是什么 其实也没什么多大要改的。海投吧
点赞 评论 收藏
分享
05-20 13:59
门头沟学院 Java
米黑子米黑子:你这个成绩不争取下保研?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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