题目描述:SQL语句查询在2025年投递简历的每个岗位,每一个月内收到简历的数目,和对应的2026年的同一个月同岗位,收到简历的数目,最后的结果先按first_year_mon月份降序,再按job降序排序显示。
原本想尝试用窗口函数直接搞定,怎么弄都不对。还是老老实实按逻辑一步一步来好了。(┭┮﹏┭┮)
with first_year as ( select job,date_format(date,"%Y-%m") as ym,sum(num) as cnt from resume_info where year(date) ="2025" group by job,ym ), second_year as ( select job,date_format(date,"%Y-%m") as ym,sum(num) as cnt from resume_info where year(date) ="2026" group by job,ym ) select fy.job, fy.ym as first_year_mon, fy.cnt as first_year_cnt, sy.ym as second_year_mon, sy.cnt as second_year_cnt from first_year fy join second_year sy on fy.job=sy.job and right(fy.ym,2)=right(sy.ym,2) order by 2 desc, 1 desc
先找出第一年2025年结果,再找出第二年2026的,然后连接就好。
注意date_format(date,"%Y-%m")无法在被year()、month()提取了。因为year()、month()只能提取完整的日期格式或时间戳。
所以这边用right()取个巧。