问题描述:请你写出SQL语句查询在2025年投递简历的每个岗位,每一个月内收到简历的数目,和对应的2026年的同一个月同岗位,收到简历的数目,最后的结果先按first_year_mon月份降序,再按job降序排序显示,以上例子查询结果如下:
图片说明

思路:创立两个表t1,t2,2025年的t1,2026年的t2
按上一道题的思路分组统计出2025年的简历数目,将date+1year新增一列,用于与表t2作为联立条件
同理分组计算出2026年的的简历数目。两者联立。排序展示

步骤
第一步:2025年分组统计

SELECT job,DATE_FORMAT(date,'%Y-%m') AS first_year_mon,
       DATE_FORMAT(DATE_ADD(date,INTERVAL 1 YEAR),'%Y-%m') AS first_year_add,SUM(num) AS first_year_cnt
FROM resume_info
WHERE YEAR(date) = 2025
GROUP BY job,first_year_mon

第二步:2026年分组统计

SELECT job,DATE_FORMAT(date,'%Y-%m') AS second_year_mon,SUM(num) AS second_year_cnt
FROM resume_info
WHERE YEAR(date) = 2026
GROUP BY job,second_year_mon)

第三步:t1,t2采用WITH子句整理,联立t1,t2,排序展示

WITH t1 AS (
        SELECT job,DATE_FORMAT(date,'%Y-%m') AS first_year_mon,
             DATE_FORMAT(DATE_ADD(date,INTERVAL 1 YEAR),'%Y-%m') AS first_year_add,SUM(num) AS first_year_cnt
       FROM resume_info
       WHERE YEAR(date) = 2025
       GROUP BY job,first_year_mon
           ),
    t2 AS (
       SELECT job,DATE_FORMAT(date,'%Y-%m') AS second_year_mon,
                SUM(num) AS second_year_cnt
      FROM resume_info
      WHERE YEAR(date) = 2026
      GROUP BY job,second_year_mon
         )

SELECT t1.job,t1.first_year_mon,t1.first_year_cnt,
       t2.second_year_mon,t2.second_year_cnt
FROM t1
LEFT JOIN t2
ON t1.job = t2.job 
    AND t1.first_year_add = t2.second_year_mon
ORDER BY t1.first_year_mon DESC,t1.job DESC;