题解 | 电商平台需要对商家的销售业绩、退款情况和客户满意度进行综合评估

电商平台需要对商家的销售业绩、退款情况和客户满意度进行综合评估

https://www.nowcoder.com/practice/48a236567617449eb6010274b30b29e8

select
a.merchant_id,
a.merchant_name,
b.total_sales_amount,
c.total_refund_amount,
d.average_satisfaction_score
from 
merchants_underline a 
left join (
    select merchant_id,
    sum(sale_amount) as total_sales_amount 
    from sales_underline 
    group by merchant_id) b on a.merchant_id=b.merchant_id 
left join (
    select merchant_id,
    sum(refund_amount) as total_refund_amount 
    from refunds_underline 
    group by merchant_id) c on b.merchant_id=c.merchant_id 
left join (
    select merchant_id,
    round(avg(satisfaction_score),2) as average_satisfaction_score
    from satisfaction_underline
    group by merchant_id
) d on c.merchant_id=d.merchant_id
order by a.merchant_id
  • 每张表的主键(sale_id/refund_id 等)都是唯一的,不会重复;但除却a表,其他表的关联字段 merchant_id却是重复的,这会导致多表连接数值膨胀。
  • 全部评论

    相关推荐

    评论
    点赞
    收藏
    分享

    创作者周榜

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