首页 > 试题广场 >

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

[编程题]电商平台需要对商家的销售业绩、退款情况和客户满意度进行综合评
  • 热度指数:1095 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
【背景】:电商平台需要对商家的销售业绩、退款情况和客户满意度进行综合评估,以确定优秀商家和需要改进的商家。
【原始表】:
merchants_underline (商家)表:
  • merchant_id (商家 ID): 商家的唯一标识符,INT
  • merchant_name (商家名称): 商家的名称,VARCHAR(50)
  • industry (行业): 商家所属的行业,VARCHAR(20)
sales_underline (销售)表:
  • sale_id (销售 ID): 销售记录的唯一标识符,INT
  • merchant_id (商家 ID): 关联商家表的商家 ID,INT
  • sale_amount (销售金额): 销售的金额,DECIMAL(10, 2)
refunds_underline (退款)表:
  • refund_id (退款 ID): 退款记录的唯一标识符,INT
  • merchant_id (商家 ID): 关联商家表的商家 ID,INT
  • refund_amount (退款金额): 退款的金额,DECIMAL(10, 2)
satisfaction_underline (满意度)表:
  • satisfaction_id (满意度 ID): 满意度记录的唯一标识符,INT
  • merchant_id (商家 ID): 关联商家表的商家 ID,INT
  • satisfaction_score (满意度得分): 客户对商家的满意度得分,INT(1 - 100 分)
【要求】:根据上述表格,查询出商家的总销售金额、总退款金额、平均满意度得分。查询结果按照商家ID升序排列。要求查询出来的表格的字段如下:
  • merchant_id: 商家 ID
  • merchant_name :商家名称。
  • total_sales_amount: 总销售金额。
  • total_refund_amount: 总退款金额。
  • average_satisfaction_score: 平均满意度得分。(round保留2位小数)
【示例】
merchants_underline (商家)表:
sales_underline (销售)表:
refunds_underline (退款)表:
satisfaction_underline (满意度)表:
【按要求查出来的表】
【解释】
上述示例中merchant_id是1的商家名称是商家A,总销售额是5000+4000 = 9000;退款总金额是1000,满意度得分是(80+70)/2 = 75
示例1

输入

DROP TABLE IF EXISTS merchants_underline;
DROP TABLE IF EXISTS sales_underline;
DROP TABLE IF EXISTS refunds_underline;
DROP TABLE IF EXISTS satisfaction_underline;
-- 创建表
CREATE TABLE merchants_underline (
    merchant_id INT PRIMARY KEY,
    merchant_name VARCHAR(50),
    industry VARCHAR(20)
);

CREATE TABLE sales_underline (
    sale_id INT PRIMARY KEY,
    merchant_id INT,
    sale_amount DECIMAL(10, 2)
);

CREATE TABLE refunds_underline (
    refund_id INT PRIMARY KEY,
    merchant_id INT,
    refund_amount DECIMAL(10, 2)
);

CREATE TABLE satisfaction_underline (
    satisfaction_id INT PRIMARY KEY,
    merchant_id INT,
    satisfaction_score INT
);

-- 插入数据
INSERT INTO merchants_underline (merchant_id, merchant_name, industry)
VALUES (1, '商家 A', '服装'),
       (2, '商家 B', '电子产品');

INSERT INTO sales_underline (sale_id, merchant_id, sale_amount)
VALUES (1, 1, 5000.00),
       (2, 2, 8000.00),
       (3, 1, 4000.00),
       (4, 2, 6000.00);

INSERT INTO refunds_underline (refund_id, merchant_id, refund_amount)
VALUES (1, 1, 1000.00),
       (2, 2, 1500.00);

INSERT INTO satisfaction_underline (satisfaction_id, merchant_id, satisfaction_score)
VALUES (1, 1, 80),
       (2, 2, 90),
       (3, 1, 70),
       (4, 2, 60);


select * from merchants_underline;
select * from sales_underline;
select * from refunds_underline;
select * from satisfaction_underline;

输出

merchant_id|merchant_name|total_sales_amount|total_refund_amount|average_satisfaction_score
1|商家 A|9000.00|1000.00|75.00
2|商家 B|14000.00|1500.00|75.00
WITH t1 AS(
    SELECT merchant_id, SUM(sale_amount) AS total_sales_amount
    FROM sales_underline
    GROUP BY merchant_id
),
t2 AS(
    SELECT merchant_id, SUM(refund_amount) AS total_refund_amount
    FROM refunds_underline
    GROUP BY merchant_id
),
t3 AS(
    SELECT merchant_id, ROUND(AVG(satisfaction_score), 2) AS average_satisfaction_score
    FROM satisfaction_underline
    GROUP BY merchant_id
)
SELECT
    mu.merchant_id,
    merchant_name,
    total_sales_amount,
    total_refund_amount,
    average_satisfaction_score
FROM merchants_underline mu
JOIN t1 ON(mu.merchant_id = t1.merchant_id)
JOIN t2 ON(mu.merchant_id = t2.merchant_id)
JOIN t3 ON(mu.merchant_id = t3.merchant_id)
ORDER BY mu.merchant_id
如果不用CTE,直接三表通过merchant_id连接会出现笛卡尔积,因为后三个表只与第一个表有关联,互相没有直接联系,就会出现全组合的多行记录。
发表于 2025-06-24 00:03:48 回复(0)
with total_sales_amount_tb as(
    select
        su.merchant_id
        , mu.merchant_name
        , round(sum(su.sale_amount), 2) as total_sales_amount
    from
        sales_underline as su
        left join merchants_underline as mu on su.merchant_id = mu.merchant_id
    group by
        su.merchant_id
        , mu.merchant_name
)
, total_refund_amount_tb as(
    select
        ru.merchant_id
        , mu.merchant_name
        , round(sum(ru.refund_amount), 2) as total_refund_amount
    from
        refunds_underline as ru
        left join merchants_underline as mu on ru.merchant_id = mu.merchant_id
    group by
        ru.merchant_id
        , mu.merchant_name
)
, average_satisfaction_score_tb as(
    select
        su.merchant_id
        , mu.merchant_name
        , round(avg(su.satisfaction_score), 2) as average_satisfaction_score
    from
        satisfaction_underline as su
        left join merchants_underline as mu on su.merchant_id = mu.merchant_id
    group by
        su.merchant_id
        , mu.merchant_name
)

select
    mu.merchant_id
    , mu.merchant_name
    , tsa_tb.total_sales_amount
    , tra_tb.total_refund_amount
    , ass_tb.average_satisfaction_score
from
    merchants_underline as mu
    left join total_sales_amount_tb as tsa_tb on mu.merchant_id = tsa_tb.merchant_id
    left join total_refund_amount_tb as tra_tb on mu.merchant_id = tra_tb.merchant_id
    left join average_satisfaction_score_tb as ass_tb on mu.merchant_id = ass_tb.merchant_id
order by
    mu.merchant_id

发表于 2025-07-01 19:49:00 回复(0)
-- 销售金额和退款金额需要独立按照merchant_id唯独求和
select
m.merchant_id,
m.merchant_name,
s.total_sales_amount,
r.total_refund_amount,
round(avg(st.satisfaction_score), 2) as average_satisfaction_score
from
merchants_underline as m
inner join (
select
merchant_id,
sum(sale_amount) as total_sales_amount
from
sales_underline
group by
merchant_id
) as s on m.merchant_id = s.merchant_id
inner join (
select
merchant_id,
sum(refund_amount) as total_refund_amount
from
refunds_underline
group by
merchant_id
) as r on s.merchant_id = r.merchant_id
inner join satisfaction_underline as st on m.merchant_id = st.merchant_id
group by
m.merchant_id,
m.merchant_name
order by
merchant_id

发表于 2025-07-01 11:54:26 回复(0)
with
    k as (
        select
            a.merchant_id,
            merchant_name,
            round(sum(sale_amount), 2) total_sales_amount
        from
            merchants_underline a
            left join sales_underline b on a.merchant_id = b.merchant_id
        group by
            a.merchant_id,
            merchant_name
    ),
    k1 as (
        select
            k.merchant_id,
            merchant_name,
            total_sales_amount,
            round(sum(refund_amount), 2) total_refund_amount
        from
            k
            left join refunds_underline c on k.merchant_id = c.merchant_id
        group by
            k.merchant_id,
            merchant_name
    )
select
    k1.merchant_id,
    merchant_name,
    total_sales_amount,
    total_refund_amount,
    round(avg(satisfaction_score), 2) average_satisfaction_score
from
    k1
    left join satisfaction_underline d on k1.merchant_id = d.merchant_id
group by
    k1.merchant_id,
    merchant_name

发表于 2025-06-30 14:14:18 回复(0)



select 
    merchant_id,
    merchant_name,
    round(sale_amount, 2) as total_sales_amount,
    round(refund_amount, 2) as total_refund_amount,
    round(avg(satisfaction_score), 2) as average_satisfaction_score
from (
    select 
        s1.merchant_id,
        m.merchant_name,
        s1.sale_amount,
        r.refund_amount,
        s2.satisfaction_score
    from sales_underline s1 left join merchants_underline m using(merchant_id)
                            left join refunds_underline r using(merchant_id)
                            left join satisfaction_underline s2 using(merchant_id)
) t
group by merchant_id, merchant_name
order by merchant_id asc
 这样子为什么不对呢?

发表于 2025-06-28 17:06:08 回复(1)
with s1 as (
    select m.merchant_id
    , m.merchant_name
    , round(avg(s.satisfaction_score), 2) average_satisfaction_score
    from merchants_underline m
    left join satisfaction_underline s 
    on m.merchant_id = s.merchant_id
    group by m.merchant_id, m.merchant_name
), 
r1 as (
    select merchant_id
    , sum(refund_amount) total_refund_amount
    from refunds_underline
    group by merchant_id
), 
s0 as (
    select merchant_id
    , sum(sale_amount) total_sales_amount
    from sales_underline
    group by merchant_id
)
select 
s1.merchant_id
, s1.merchant_name
, s0.total_sales_amount
, r1.total_refund_amount
, s1.average_satisfaction_score
from s1
left join s0 on s1.merchant_id = s0.merchant_id
left join r1 on s1.merchant_id = r1.merchant_id
order by s1.merchant_id

个人思路:

  1. 将merchant_underline表与satisfaction_underline表左连接(防止出现有 merchant_id 却没有满意度评分的商家),按merchant_underline中的 merchant_id 分组,输出每个 merchant_id 的平均满意度(保留两位小数)以及对应的 merchant_name,记为 s1
  2. 将refunds_underline按照 merchant_id 分组,输出每个出现在该表中的 merchant_id 的总退款金额,记为r1
  3. 将sales_underline按照 merchant_id 分组,输出每个出现在该表中的 merchant_id 的总销售金额,记为s0
  4. 将 s0 与 s0、r1 分别按照关键字 merchant_id 左连接
  5. 输出题目对应的五个指标,并按照 s1.merchant_id 升序排列
发表于 2025-06-27 20:33:09 回复(1)
select m.merchant_id,
       m.merchant_name,
       round(sum(s.sale_amount),2) as total_sales_amount,
       round(sum(r.refund_amount),2) as total_refund_amount,
       round(avg(u.satisfaction_score),2) as aveage_satifaction_score

from sales_underline as s
left join merchants_underline as m 
  on s.merchant_id=m.merchant_id
left join refunds_underline as r 
  on s.merchant_id=r.merchant_id
left join satisfaction_underline  as u
   on s.merchant_id=u.merchant_id

group by m.merchant_id, m.merchant_name
order by m.merchant_id
错误的答案,第3、第4字段结果会虚高,使用多个left join,且连接条件均为merchant_id行重复(笛卡尔积)导致聚合函数计算错误,只能每个聚合字段分开算,然后主查询在一起。
发表于 2025-06-23 18:44:50 回复(1)
with table1 as
(
    select
        merchant_id ,
        sum(sale_amount) as total_sales_amount
    from
        sales_underline
    group by
        merchant_id
),
table2 as
(
    select
        merchant_id,
        round(avg(satisfaction_score),2) as average_satisfaction_score
    from
        satisfaction_underline
    group by
        merchant_id
),
table3 as
(
    select
        merchant_id,
        sum(refund_amount) as total_refund_amount
    from
        refunds_underline
    group by
        merchant_id
)
select  
    me.merchant_id,
    me.merchant_name ,
    t1.total_sales_amount,
    t3.total_refund_amount,
    t2.average_satisfaction_score
from
    merchants_underline  me
join
    table1 t1 on me.merchant_id = t1.merchant_id
join
    table2 t2 on me.merchant_id = t2.merchant_id
join
    table3 t3 on me.merchant_id = t3.merchant_id
order by
    me.merchant_id

发表于 2025-06-23 11:13:20 回复(0)
with t1 as(select merchant_id, sum(sale_amount) total_sales_amount
from sales_underline
group by merchant_id),
t2 as(select merchant_id, sum(refund_amount) total_refund_amount
from refunds_underline
group by merchant_id),
t3 as (select merchant_id, round(avg(satisfaction_score),2) average_satisfaction_score
from satisfaction_underline
group by merchant_id)
select mu.merchant_id merchant_id,
merchant_name,
total_sales_amount,
total_refund_amount,
average_satisfaction_score
from merchants_underline mu
join t1  on mu.merchant_id=t1.merchant_id
join t2 on mu.merchant_id=t2.merchant_id
join t3 on mu.merchant_id=t3.merchant_id
order by merchant_id
发表于 2025-06-21 19:43:37 回复(0)
WITH
  temp1 AS(
    SELECT merchant_id, SUM(sale_amount) AS total_sales_amount
    FROM sales_underline
    GROUP BY merchant_id
  ),
  temp2 AS(
    SELECT merchant_id, SUM(refund_amount) AS total_refund_amount
    FROM refunds_underline
    GROUP BY merchant_id
  ),
  temp3 AS(
    SELECT 
      merchant_id, 
      ROUND(AVG(satisfaction_score), 2) AS average_satisfaction_score
    FROM satisfaction_underline 
    GROUP BY merchant_id
  )
SELECT
  mu.merchant_id,
  merchant_name,
  total_sales_amount,
  total_refund_amount,
  average_satisfaction_score
FROM merchants_underline mu
JOIN temp1 USING (merchant_id)
JOIN temp2 USING (merchant_id)
JOIN temp3 USING (merchant_id)
ORDER BY mu.merchant_id

发表于 2025-06-19 11:15:02 回复(0)
SELECT A.merchant_id,
       merchant_name,
       total_sales_amount,
       total_refund_amount,
       average_satisfaction_score
FROM (SELECT MU.merchant_id,
             merchant_name,
             ROUND(SUM(sale_amount),2) AS total_sales_amount
       FROM merchants_underline AS MU
       JOIN sales_underline AS SU ON SU.merchant_id = MU.merchant_id
       JOIN refunds_underline AS RU ON RU.merchant_id = MU.merchant_id
       GROUP BY MU.merchant_id
) AS A
JOIN (SELECT MU.merchant_id,
             ROUND(SUM(refund_amount),2) AS total_refund_amount
      FROM merchants_underline AS MU
      JOIN refunds_underline AS RU ON RU.merchant_id = MU.merchant_id
       GROUP BY MU.merchant_id
             ) AS B ON B.merchant_id = A.merchant_id
JOIN (SELECT MU.merchant_id,
             ROUND(AVG(satisfaction_score),2) AS average_satisfaction_score
      FROM satisfaction_underline AS SAU
      JOIN merchants_underline AS MU ON SAU.merchant_id = MU.merchant_id  
      GROUP BY MU.merchant_id    
) AS C ON C.merchant_id = A.merchant_id
ORDER BY A.merchant_id;
发表于 2025-06-17 19:43:43 回复(0)
不知道哪里错了,各位帮看看
with t1
as(
    select
    a.merchant_id,
    merchant_name,
    sum(sale_amount) as total_sales_amount
    from sales_underline a
    left join merchants_underline b
    on a.merchant_id = b.merchant_id
    group by 1,2
),
t2
as(
    select
    merchant_id,
    avg(satisfaction_score) as avergae_satisfaction_score
    from satisfaction_underline
    group by 1
),
t3
as(
    select
    merchant_id,
    sum(refund_amount) as total_refund_amount
    from refunds_underline
    group by 1
)

select
t1.merchant_id,
merchant_name,
total_sales_amount,
total_refund_amount,
avergae_satisfaction_score
from t1
left join t2
on t1.merchant_id = t2.merchant_id
left join t3
on t1.merchant_id = t3.merchant_id
order by 1
发表于 2025-06-17 17:35:16 回复(1)
with t1 as(
select merchant_id, sum(sale_amount)  as total_sales_amount
from sales_underline
group by merchant_id),
t2 as(
select merchant_id,round(avg(satisfaction_score) ,2) as average_satisfaction_score
from satisfaction_underline
group by merchant_id
)
select merchant_id,merchant_name,total_sales_amount,refund_amount as total_refund_amount,average_satisfaction_score
from merchants_underline
join refunds_underline using(merchant_id)
join t1 using(merchant_id)
join t2 using(merchant_id)

发表于 2025-06-15 00:55:43 回复(0)