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

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

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

# 指标:商品毛利率=(1-进价总额/销售总额)*100%
#      店铺毛利率=(1-总进价成本/总销售收入)*100%。
# 方法1:union
# 1.商品毛利率
# select tpi.product_id,concat(round((1-sum(in_price*cnt)/sum(price*cnt))*100,1),'%')
# from tb_product_info tpi
# inner join tb_order_detail tod
# on tpi.product_id=tod.product_id
# inner join tb_order_overall too
# on tod.order_id=too.order_id
# where shop_id='901'
# and date(event_time) >= "2021-10-01"
# group by product_id
# # 2.店铺毛利率
# select '商铺汇总' as product_id,concat(round((1-sum(in_price*cnt)/sum(price*cnt))*100,1),'%')
# from tb_product_info tpi
# inner join tb_order_detail tod
# on tpi.product_id=tod.product_id
# inner join tb_order_overall too
# on tod.order_id=too.order_id
# where shop_id='901'
# and date(event_time) >= "2021-10-01"
# 3.整合
# select '店铺汇总' as product_id,concat(round((1-sum(in_price*cnt)/sum(price*cnt))*100,1),'%')
# from tb_product_info tpi
# inner join tb_order_detail tod
# on tpi.product_id=tod.product_id
# inner join tb_order_overall too
# on tod.order_id=too.order_id
# where shop_id='901'
# and date(event_time) >= "2021-10-01"
# union
# select tpi.product_id,concat(round((1-sum(in_price*cnt)/sum(price*cnt))*100,1),'%')
# from tb_product_info tpi
# inner join tb_order_detail tod
# on tpi.product_id=tod.product_id
# inner join tb_order_overall too
# on tod.order_id=too.order_id
# where shop_id='901'
# and date(event_time) >= "2021-10-01"
# group by product_id
# having 1-sum(in_price*cnt)/sum(price*cnt)>0.249
# 方法2:ifnull+ with rollup
SELECT product_id, CONCAT(profit_rate, "%") as profit_rate
FROM (
    SELECT IFNULL(product_id, '店铺汇总') as product_id,
        ROUND(100 * (1 - SUM(in_price*cnt) / SUM(price*cnt)), 1) as profit_rate
    FROM (
        SELECT product_id, price, cnt, in_price
        FROM tb_order_detail
        JOIN tb_product_info USING(product_id)
        JOIN tb_order_overall USING(order_id)
        WHERE shop_id = 901 and DATE(event_time) >= "2021-10-01"
    ) as t_product_in_each_order
    GROUP BY product_id
    WITH ROLLUP
    HAVING profit_rate > 24.9 OR product_id IS NULL
    ORDER BY product_id
) as t1;

全部评论

相关推荐

12-24 20:52
武汉大学 Java
点赞 评论 收藏
分享
12-18 18:50
已编辑
门头沟学院 golang
牛客33637108...:重点是要事已密成,在没有进入这家公司之前,不要有任何的泄露信息,我之前跟你一样,面了一家光伏设备厂,底薪7500加上出差补贴大概有13,000左右,已经给了口头offer了,甚至要了我的在校成绩的所有信息,还向我要了三方的网签二维码,到后面还是毁约了,我干过最愚蠢的事情就是向同学透露要签三方的事,之后的失败只会让他们幸灾乐祸,这是即将结束的大学生活给我的最后一课,不要相信任何的口头三方,该面的就去面,甚至签了三方也有毁约的可能,就像我现在签了三方还在外面实习呢,春招还是要继续参加的,不能停止面试,不然到后面毁三方的时候,重新捡起的面试很麻烦的,这是我一点点小小的见解。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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