首页 > 试题广场 >

获取指定客户每月的消费额

[编程题]获取指定客户每月的消费额
  • 热度指数:50030 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
某金融公司某项目下有如下 2 张表:
交易表 trade(t_id:交易流水号,t_time:交易时间,t_cus:交易客户,t_type:交易类型【1表示消费,0表示转账】,t_amount:交易金额):
t_id t_time t_cus t_type t_amount
1 2022-01-19 03:14:08 101 1 45.0
2 2023-02-15 11:22:11 101 1 23.6
3 2023-03-19 05:33:22 102 0 350.0
4 2023-03-21 06:44:09 103 1 16.9
5 2023-02-21 08:44:09 101 1 26.9
6 2023-07-07 07:11:45 101 1 1200.0
7 2023-07-19 06:04:32 102 1 132.5
8 2023-09-19 11:23:11 101 1 130.6
9 2023-10-19 04:32:30 103 1 110.0

客户表 customer(c_id:客户号,c_name:客户名称):
c_id c_name
101 Tom
102 Ross
103 Julie
104 Niki
现需要查询 Tom 这个客户在 2023 年每月的消费金额(按月份正序显示),示例如下:
time total
2023-02 50.5
2023-07 1200.0
2023-09 130.6
请编写 SQL 语句实现上述需求。
示例1

输入

drop table if exists  `trade` ; 
CREATE TABLE `trade` (
`t_id` int(11) NOT NULL,
`t_time` TIMESTAMP NOT NULL,
`t_cus` int(16) NOT NULL,
`t_type` int(2) NOT NULL,
`t_amount` double NOT NULL,
PRIMARY KEY (`t_id`));
INSERT INTO trade VALUES(1,'2022-01-19 03:14:08',101,1,45);
INSERT INTO trade VALUES(2,'2023-02-15 11:22:11',101,1,23.6);
INSERT INTO trade VALUES(3,'2023-03-19 05:33:22',102,0,350);
INSERT INTO trade VALUES(4,'2023-03-21 06:44:09',103,1,16.9);
INSERT INTO trade VALUES(5,'2023-02-21 08:44:09',101,1,26.9);
INSERT INTO trade VALUES(6,'2023-07-07 07:11:45',101,1,1200);
INSERT INTO trade VALUES(7,'2023-07-19 06:04:32',102,1,132.5);
INSERT INTO trade VALUES(8,'2023-09-19 11:23:11',101,1,130.6);
INSERT INTO trade VALUES(9,'2023-10-19 04:32:30',103,1,110);

drop table if exists  `customer` ;   
CREATE TABLE `customer` (
`c_id` int(11) NOT NULL,
`c_name` varchar(20) NOT NULL,
PRIMARY KEY (`c_id`));
INSERT INTO customer VALUES(101,'Tom');
INSERT INTO customer VALUES(102,'Ross');
INSERT INTO customer VALUES(103,'Juile');
INSERT INTO customer VALUES(104,'Niki');

输出

time|total
2023-02|50.5
2023-07|1200.0
2023-09|130.6
select
    DATE_FORMAT (a.t_time, '%Y-%m') time,
    sum(a.t_amount) total
from
    trade a
    join customer b on a.t_cus = b.c_id
where
    b.c_name = 'Tom'
    and a.t_type = 1
GROUP BY
    DATE_FORMAT (a.t_time, '%Y-%m');

发表于 2024-07-22 21:41:28 回复(2)
select date_format(`t_time`,"%Y-%m") as `time`, sum(`t_amount`) as `total` from `trade` 
where year(`t_time`)=2023 
and `t_type`=1 
and `t_cus`in (select `c_id` from `customer` where`c_name`="Tom")  
group by date_format(`t_time`,"%Y-%m") 
order by date_format(`t_time`,"%Y-%m");
发表于 2024-05-20 19:09:45 回复(3)

方法一:date_format

select 
    date_format(t_time,'%Y-%m') time,
    sum(t_amount) total
from 
    customer c left join trade t on c.c_id=t.t_cus
where 
    c_name='Tom' and t_type=1
group by time
having time=2023
order by time;


方法二:截取

select 
    left(t_time,7) time,
    sum(t_amount) total
from trade t
     join customer c on t.t_cus=c.c_id
where 
    c_name='Tom' and t_type=1 and year(t_time)=2023
group by time
order by time;


发表于 2024-12-17 15:59:31 回复(1)
select
    date_format(t_time, '%Y-%m') as time
    ,sum(t_amount) as total
from trade t
left join customer c
     on c.c_id = t.t_cus
where c.c_name = 'Tom' and year(t.t_time) = 2023 and t.t_type = 1
group by time
order by time

发表于 2024-11-21 21:51:50 回复(1)
select substring_index(t_time,'-',2) time,sum(t_amount) total
from trade t,customer c
where t.t_cus=c.c_id and c_name='Tom' and substring_index(t_time,'-',1)='2023' and t_type=1
group by substring_index(t_time,'-',2)
order by time
发表于 2024-09-26 10:49:51 回复(1)
select
    date_format(t_time, '%Y-%m') time,
    sum(t_amount) total
from  trade t
join customer c on t.t_cus = c.c_id
where c.c_name = 'Tom'
and year(t.t_time) = '2023'
and t.t_type = 1
group by time
order by time
发表于 2025-03-22 21:11:21 回复(0)
求助一下 为什么我这里错了?我想不明白哪里错了
with
    t1 as (
        select
            t_id,
            t_time,
            t_cus,
            t_type,
            t_amount,
            date_format (t_time, '%Y-%m') as time
        from
            trade
            where  t_type = 1
    ),
    t2 as (
        select
            c_id,
            c_name
        from
            customer
        where
            c_name = 'Tom'
    )

selec
t1.timd,sum (t1.t_amount) as total
from
join t2 on t1.t_ cus = t2.c id
where
year (t1.time ) = 2023
group by time
order by total

发表于 2025-01-26 17:02:52 回复(2)
select
date_format(t_time,'%Y-%m') time,
sum(t_amount) total
from
trade t
left join
customer c
on t.t_cus=c.c_id
where c_name='Tom'
and year(t_time)=2023
and t_type=1
group by date_format(t_time,'%Y-%m')
order by date_format(t_time,'%Y-%m')
还是那句话,自测运行和保存提交的数据库是不一样的,要严格按照题目要求写出来才是对的
发表于 2024-12-05 17:35:32 回复(0)
select date_format(t.t_time, '%Y-%m') as 'time', round(sum(t.t_amount), 1) as total
from trade t left join customer c on t.t_cus = c.c_id
where year(t.t_time) = '2023' and t.t_type = 1 and c.c_name = 'Tom'
group by date_format(t.t_time, '%Y-%m')
order by date_format(t.t_time, '%Y-%m')
发表于 2024-08-29 15:05:08 回复(0)

WITH monthdata AS
(
    SELECT DATE_FORMAT(t_time, '%Y-%m') time, t_amount
    FROM trade
    WHERE t_cus IN(
        SELECT c_id FROM customer WHERE c_name = 'Tom'
    )
    AND t_type = 1
    AND YEAR(t_time) = 2023
)

SELECT time, SUM(t_amount) total
FROM monthdata
GROUP BY time
ORDER BY time

发表于 2025-05-30 21:30:28 回复(0)
select date_format(t.t_time,'%Y-%m') time,
round(sum(t.t_amount),1) as total
from trade t join customer c on t.t_cus=c.c_id 
where c.c_name="Tom" and year(t.t_time)='2023' and t.t_type=1
group by date_format(t.t_time,'%Y-%m')
order by date_format(t.t_time,'%Y-%m')

发表于 2025-05-29 16:06:17 回复(0)
with
    k as (
        select
            DATE_FORMAT (t_time, '%Y-%m') AS time,
            t_cus,
            sum(t_amount) total
        from
            trade a
            left join customer b on a.t_cus = b.c_id
        where
            year (t_time) = 2023
            and c_name = 'Tom'
            and t_type = 1
        group by
            DATE_FORMAT (t_time, '%Y-%m'),
            t_cus
        order by
            DATE_FORMAT (t_time, '%Y-%m')
    )
select
    time,
    total
from
    k

发表于 2025-05-28 11:54:21 回复(0)
SELECT trade1.time,
       SUM(t_amount) AS total
FROM 
  (SELECT DATE_FORMAT(t_time,'%Y-%m') AS time,--%m要小写,大写输出英文
          t_type,
          t_amount,
          t_cus,
          t_time
   FROM trade
  ) AS trade1
 LEFT JOIN
     customer
 ON trade1.t_cus=customer.c_id
WHERE YEAR(trade1.t_time)=2023--这里不能写trade1_time
  AND t_type=1
  AND c_name='TOM'
GROUP BY trade1.time
ORDER BY trade1.time;

发表于 2025-05-24 15:42:23 回复(0)
select
    t1.time
    , sum(t_amount) as total
from
    (select
        substr(t.t_time, 1, 7) as time
        , t.t_amount
    from
        trade as t
        left join customer as c on t.t_cus = c.c_id
    where
        c.c_name = 'Tom'
        and year(t.t_time) = 2023
        and t_type = 1
    ) as t1
group by
    time
order by
    time

发表于 2025-05-18 17:53:47 回复(0)
select
left(t_time,7) as time,
sum(t_amount)  as total
from trade join customer on trade.t_cus = customer.c_id
where c_name='Tom' and year(t_time)=2023 and t_type=1
group by left(t_time,7)
order by time asc

发表于 2025-05-18 12:12:24 回复(0)
select t_time_1 as time,sum(t_amount) as total
from ( select substr(t_time,1,7) t_time_1 ,t_amount
        from trade
        inner join customer on trade.t_cus=customer.c_id and customer.c_name='Tom'and year(t_time)='2023' and t_type=1) t1
group by t1.t_time_1
order by t_time_1;

发表于 2025-04-29 17:48:33 回复(0)
WITH xf as(
SELECT DATE_FORMAT(t.t_time,'%Y-%m') as time, sum(t.t_amount) as total
from trade t LEFT join customer c on t.t_cus = c.c_id 
where c.c_name = 'Tom' 
and  YEAR(DATE_FORMAT(t.t_time, '%Y-%m-%d')) = 2023
and  t.t_type = 1
GROUP BY DATE_FORMAT(t.t_time,'%Y-%m')
)
SELECT time,total from xf  ORDER BY time;
发表于 2025-04-18 22:19:58 回复(0)
select
substring_index(t_time,'-',2) as time,
# date_format(t_time,'%Y-%m') as time,
round(sum(t_amount),1) as total
from 
trade a join  customer b on a.t_cus=b.c_id
where b.c_name='Tom' and a.t_type=1
and substring_index(t_time,'-',1)=2023 
group by time
# having time=2023
order by time ASC;

发表于 2025-04-17 14:49:01 回复(0)
select
left(t_time,7) time	
,sum(t_amount) total
from 
trade
join customer on
trade.t_cus=customer.c_id
where c_name='Tom'
and left(t_time,4)='2023'
and t_type=1
group by 1
order by time

发表于 2025-04-15 15:40:22 回复(0)
select left(t_time,7)as time, sum(t_amount) as total
from trade a
left join customer b
on a.t_cus=b.c_id
where c_name='Tom' and t_type=1 and year(t_time)=2023
group by time
order by time
发表于 2025-04-10 15:23:59 回复(0)