首页 > 试题广场 >

物流公司想要分析快递小哥的收入情况

[编程题]物流公司想要分析快递小哥的收入情况
  • 热度指数:4306 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
【背景】:物流公司想要分析快递小哥的薪资构成和绩效情况,以便制定更合理的薪酬政策。
【原始表】:
couriers_info (快递员)表:
  • courier_id (快递员 ID): 快递员的唯一标识符,INT
  • courier_name (快递员姓名): 快递员的姓名,VARCHAR(50)
  • base_salary (基本工资): 快递员的基本工资,DECIMAL(10, 2)
deliveries_info  (派送)表:
  • delivery_id (派送 ID): 派送任务的唯一标识符,INT
  • courier_id (快递员 ID): 关联快递员表的快递员 ID,INT
  • delivery_date (派送日期): 派送的日期,DATE
  • delivery_fee (派送费用): 每次派送的费用,DECIMAL(10, 2)

【要求】:根据上述表格,查询出每个快递员在 2024 年 7 月的总收入(基本工资 + 派送费用总和 )。查询结果按照快递员 ID 升序排列。要求查询出来的表格的字段如下:
  • courier_id: 快递员的唯一标识符。
  • courier_name: 快递员的姓名。
  • total_income: 快递员2024 年 7 月的总收入。
【示例】
couriers_info (快递员)表:
deliveries_info  (派送)表:
【按要求查出来的表】
【解释】
上述示例中,courier_id是2的员工名字是Bob,底薪是1800,和他有关的派送费用有3笔但是只有2笔是7月份的,所以他7月份的收入是1800+60+60 = 1920
示例1

输入

DROP TABLE IF EXISTS couriers_info;
DROP TABLE IF EXISTS deliveries_info;


CREATE TABLE couriers_info (
    courier_id INT PRIMARY KEY,
    courier_name VARCHAR(50),
    base_salary DECIMAL(10, 2)
);

CREATE TABLE deliveries_info (
    delivery_id INT PRIMARY KEY,
    courier_id INT,
    delivery_date DATE,
    delivery_fee DECIMAL(10, 2)
);



INSERT INTO couriers_info (courier_id, courier_name, base_salary) VALUES
(1, 'Alice', 2000.00),
(2, 'Bob', 1800.00);

INSERT INTO deliveries_info (delivery_id, courier_id, delivery_date, delivery_fee) VALUES
(1, 1, '2024-07-01', 50.00),
(2, 1, '2024-07-05', 50.00),
(3, 2, '2024-06-03', 40.00),
(4, 2, '2024-07-10', 60.00),
(5, 2, '2024-07-10', 60.00);


select * from couriers_info;
select * from deliveries_info;

输出

courier_id|courier_name|total_income
1|Alice|2100.00
2|Bob|1920.00
使用COALESCE完成

SELECT 
    c.courier_id,
    c.courier_name,
    c.base_salary + COALESCE(SUM(d.delivery_fee), 0) AS total_income
FROM 
    couriers_info c
LEFT JOIN 
    deliveries_info d 
ON 
    c.courier_id = d.courier_id
    AND d.delivery_date BETWEEN '2024-07-01' AND '2024-07-31'  -- 筛选2024年7月的派送记录
GROUP BY 
    c.courier_id, c.courier_name, c.base_salary  -- 按快递员分组(需包含SELECT中非聚合字段)
ORDER BY 
    c.courier_id ASC;  -- 按快递员ID升序排列


发表于 2025-08-02 19:50:10 回复(0)
select
x.courier_id
,x.courier_name
,round(avg(base_salary)+sum(delivery_fee),2) as total_income
from couriers_info  x
join deliveries_info  y 
using(courier_id)
where left(delivery_date,7)="2024-07"
group by 1,2
order by 1

发表于 2025-10-17 12:56:14 回复(0)
select t1.courier_id,courier_name,(base_salary+sum(delivery_fee)) as total_income
from couriers_info t1
join deliveries_info t2
on t1.courier_id=t2.courier_id and delivery_date between "2024-07-01" and "2024-07-31"
group by t1.courier_id,courier_name
order by t1.courier_id;

发表于 2026-01-20 14:36:36 回复(0)
select ci.courier_id,courier_name,(base_salary+sum(delivery_fee))total_income
from couriers_info ci join deliveries_info di on ci.courier_id=di.courier_id
where month(delivery_date)=7
group by courier_id
order by ci.courier_id
发表于 2026-01-13 16:39:07 回复(0)
select
    d.courier_id,
    courier_name,
    base_salary + sum(delivery_fee) total_income
from
    deliveries_info d
    join couriers_info c on d.courier_id = c.courier_id
where
    delivery_date between '2024-07-01' and '2024-07-31'
group by
    d.courier_id,
    courier_name,
    base_salary
order by
    d.courier_id
发表于 2026-01-04 22:02:02 回复(0)
WITH t as (
    SELECT courier_id, sum(delivery_fee) as tot_fee
    FROM deliveries_info
    WHERE delivery_date like '2024-07%'
    GROUP BY courier_id
)
SELECT c.courier_id, c.courier_name, (t.tot_fee + c.base_salary) as total_income
FROM t 
LEFT JOIN couriers_info c ON c.courier_id = t.courier_id
ORDER BY courier_id

发表于 2026-01-04 16:34:40 回复(0)
WITH a AS (
    select courier_id,
        sum(delivery_fee) as sm
    from deliveries_info
    where substr(delivery_date,1,7) = "2024-07"
    group by courier_id
)
select courier_id,
    courier_name,
    (sm + base_salary) as total_income
from a
    join couriers_info using(courier_id)
order by courier_id


发表于 2025-12-17 21:39:19 回复(0)
select
    c.courier_id,
    courier_name,
    base_salary+sum(delivery_fee) total_income
from 
    couriers_info as c 
    left join deliveries_info as d on c.courier_id=d.courier_id
where
    delivery_date between '2024-07-01' and '2024-07-31'
group by 
    c.courier_id,
    courier_name
order by
    courier_id 

这个题目好像之前已经出现过一模一样的了

发表于 2025-12-17 21:14:33 回复(1)
WITH
t1 AS (     -- 计算员工7月提升
SELECT
    courier_id,
    SUM(delivery_fee) AS tc
FROM deliveries_info
WHERE delivery_date >= '2024-07-01' AND delivery_date < '2024-08-01'
GROUP BY courier_id
)

SELECT      -- 主查询,计算底薪+提成
    a.courier_id,
    a.courier_name,
    a.base_salary + t1.tc AS total_income
FROM couriers_info AS a
INNER JOIN t1
    ON a.courier_id = t1.courier_id
ORDER BY a.courier_id;
发表于 2025-11-26 18:06:44 回复(0)
select c.courier_id,courier_name,base_salary+sum(delivery_fee) total_income from couriers_info c
join deliveries_info d on c.courier_id=d.courier_id
where delivery_date between "2024-07-01" and "2024-07-31"
group by c.courier_id
order by c.courier_id
发表于 2025-11-25 14:39:37 回复(0)
select couriers_info.courier_id as courier_id,courier_name,base_salary+sum(delivery_fee) as total_income
from couriers_info
left join deliveries_info d on d.courier_id=couriers_info.courier_id
where delivery_date like '2024-07%'
group by couriers_info.courier_id,courier_name
order by courier_id

发表于 2025-10-22 15:51:57 回复(0)
这题做过 类似的吧。题目有点重复了,质量有点不高
select courier_id, courier_name, round((base_salary+fees),2) total_income
from
couriers_info
join (select courier_id, sum(delivery_fee) fees
from deliveries_info
group by courier_id) f1
using(courier_id)
order by courier_id asc;
发表于 2025-10-09 12:13:54 回复(0)
SELECT
    c.courier_id,
    c.courier_name,
    ROUND(c.base_salary + COALESCE(SUM(d.delivery_fee),0) ,2) total_income
FROM couriers_info c 
LEFT JOIN deliveries_info d 
ON c.courier_id = d.courier_id
WHERE d.delivery_date BETWEEN '2024-07-01' AND '2024-07-30'
GROUP BY c.courier_id
ORDER BY c.courier_id;

发表于 2025-09-11 16:02:52 回复(0)
select c.courier_id, c.courier_name, sum(d.delivery_fee) + any_value(base_salary) total_income
from couriers_info c join deliveries_info d using(courier_id)
where date_format(d.delivery_date,'%Y-%m') = '2024-07'
group by c.courier_id
order by c.courier_id
发表于 2025-08-02 10:45:01 回复(0)
SELECT
    d.courier_id,
    courier_name,
    base_salary + SUM(delivery_fee) AS total_income
FROM couriers_info c
JOIN deliveries_info d USING (courier_id)
WHERE DATE_FORMAT(delivery_date,'%Y-%m') = '2024-07'
GROUP BY d.courier_id
ORDER BY d.courier_id
发表于 2025-07-25 14:43:16 回复(0)
select c.courier_id,
       c.courier_name,
       (c.base_salary + sum(d.delivery_fee)) as total_income

from couriers_info as c
left join deliveries_info as d
on c.courier_id =d.courier_id 
 and date_format(delivery_date,'%Y-%m')='2024-07'

group by c.courier_id, c.courier_name
order by c.courier_id

发表于 2025-06-27 15:58:46 回复(0)

SELECT
c.courier_id,
c.courier_name,
ROUND(
(c.base_salary + sum(d.delivery_fee)), 2
) AS total_income

FROM couriers_info AS c
LEFT JOIN deliveries_info AS d ON c.courier_id = d.courier_id
WHERE d.delivery_date BETWEEN '2024-07-01' AND '2024-07-31'
GROUP BY c.courier_id, c.courier_name, c.base_salary
ORDER BY c.courier_id ASC


发表于 2025-06-15 20:29:20 回复(0)