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

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

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

# 请计算2021年10月以来店铺901中商品毛利率大于24.9%的商品信息及店铺整体毛利率
with
    a as (
        select
            product_id,
            in_price,
            price,
            cnt
        from
            tb_product_info
            join tb_order_detail using (product_id)
            join tb_order_overall using (order_id)
        where
            date (event_time) >= '2021-10-01'
            and shop_id = 901
    ),
    b as (
        (
             # 店铺毛利率=(1-总进价成本/总销售收入)*100%
            select
                '店铺汇总' product_id,
                round(
                    (1 - sum(in_price * cnt) / sum(price * cnt)) * 100,
                    1
                ) profit_rate
            from
                a
        )
        union
        (
            # 商品毛利率=(1-进价/平均单件售价)*100%
            select
                product_id,
                round(
                    (1 - avg(in_price) / (sum(price * cnt) / sum(cnt))) * 100,
                    1
                ) profit_rate
            from
                a
            group by
                product_id
            having
                profit_rate > 24.9
            order by
                product_id
        )
    )
select
    product_id,
    concat (profit_rate, "%") profit_rate
from
    b

全部评论

相关推荐

重生我想学测开:嵌入式的问题,我准备入行京东外卖了
点赞 评论 收藏
分享
05-14 20:34
门头沟学院 Java
窝补药贝八股:管他们,乱说,反正又不去,直接说680
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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