题解 | #获取指定客户每月的消费额#

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

https://www.nowcoder.com/practice/ed04f148b63e469e8f62e051d06a46f5

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

用到了date_format函数

-- 获取年 2022
SELECT DATE_FORMAT('2022-05-25 09:25:11', '%Y');
-- 获取年月 2022-05
SELECT DATE_FORMAT('2022-05-25 09:25:11', '%Y-%m');
-- 获取月 05
SELECT DATE_FORMAT('2022-05-25 09:25:11', '%m');
-- 获取年月日 2022-05-25
SELECT DATE_FORMAT('2022-05-25 09:25:11', '%Y-%m-%d');
-- 获取时分秒 09:25:11
SELECT DATE_FORMAT('2022-05-25 09:25:11', '%H:%i:%s');

全部评论

相关推荐

点赞 评论 收藏
分享
真烦好烦真烦:豆包润色了自己没看看吗,再说了,都说豆包是愚蠢且勤快的大学生,ds才是聪明的研究生,怎么敢让豆包写论文的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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