# 请你写出SQL语句查询在2025年投递简历的每个岗位,每一个月内收到简历的数目,和对应的2026年的同一个月同岗位,收到简历的数目,最后的结果先按first_year_mon月份降序,再按job降序排序显示.
# 思路:两次查询统计出2025年的信息和2026年的信息,然后用月份和岗位作为连接键连接两张表。
with
-- 创建临时表 a,用于存储 2025 年的信息
a as
(
select job,date_format(date,'%Y-%m') as first_year_mon,sum(num) as first_year_cnt
from resume_info
-- 筛选出日期中左边四位为 2025 的记录,即 2025 年的记录
where left(date,4) = 2025
group by job,first_year_mon
),
-- 创建临时表 b,用于存储 2026 年的信息
b as
(
select job,date_format(date,'%Y-%m') as second_year_mon,sum(num) as second_year_cnt
from resume_info
-- 筛选出日期中左边四位为 2026 的记录,即 2026 年的记录
where left(date,4) = 2026
group by job,second_year_mon
)
-- 从临时表 a 和 b 中查询所需信息,并进行连接
select a.job
,first_year_mon
,first_year_cnt
,second_year_mon
,second_year_cnt
from a join b on a.job=b.job and right(first_year_mon,2)=right(second_year_mon,2)
-- 按照 first_year_mon 降序和 job 降序进行排序
order by first_year_mon desc,a.job desc;