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

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

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

全部评论

相关推荐

2024-12-21 10:42
已编辑
江西软件职业技术大学 Java
新宿站不停:该提升学历就提升学历,菜了就多练。没事找牛马公司虐自己是吧? 谁没事说自己“经验少”,这不自己把自己塞剎鼻hr嘴里找🐴吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务