列表

详情


SQL280. 实习广场投递简历分析(三)

描述

在牛客实习广场有很多公司开放职位给同学们投递,同学投递完就会把简历信息存到数据库里。
现在有简历信息表(resume_info),部分信息简况如下:
id job date num
1 C++ 2025-01-02 53
2 Python 2025-01-02
23
3 Java 2025-01-02
12
4 C++ 2025-01-03
54
5 Python
2025-01-03
43
6 Java
2025-01-03
41
7 Java
2025-02-03
24
8 C++
2025-02-03
23
9 Python
2025-02-03 34
10 Java
2025-02-04
42
11
C++
2025-02-04
45
12 Python
2025-02-04
59
13
C++
2026-01-04
230
14 Java
2026-01-04 764
15 Python
2026-01-04
644
16 C++
2026-01-06
240
17 Java
2026-01-06
714
18 Python
2026-01-06
624
19 C++
2026-01-04
260
20 Java
2026-02-14 721
21 Python
2026-02-14
321
22 C++
2026-02-14
134
23 Java
2026-02-24
928
24 Python
2026-02-24
525
25 C++
2027-02-06
231

第1行表示,在2025年1月2号,C++岗位收到了53封简历
......
最后1行表示,在2027年2月6号,C++岗位收到了231封简历

请你写出SQL语句查询在2025年投递简历的每个岗位,每一个月内收到简历的数目,和对应的2026年的同一个月同岗位收到简历的数目,最后的结果先按first_year_mon月份降序,再按job降序排序显示,以上例子查询结果如下:
job first_year_mon first_year_cnt second_year_mon
second_year_cnt
Python 2025-02 93 2026-02
846
Java 2025-02
66 2026-02
1649
C++ 2025-02
68 2026-02
394
Python
2025-01
66 2026-01
1268
Java
2025-01
53 2026-01
1478
C++
2025-01
107 2026-01
470
解析:
第1行表示Python岗位在2025年2月收到了93份简历,在对应的2026年2月收到了846份简历
......
最后1行表示C++岗位在2025年1月收到了107份简历,在对应的2026年1月收到了470份简历

示例1

输入:

drop table if exists resume_info;
CREATE TABLE resume_info (
id int(4) NOT NULL,
job varchar(64) NOT NULL,
date date NOT NULL,
num int(11) NOT NULL,
PRIMARY KEY (id));

INSERT INTO resume_info VALUES
(1,'C++','2025-01-02',53),
(2,'Python','2025-01-02',23),
(3,'Java','2025-01-02',12),
(4,'C++','2025-01-03',54),
(5,'Python','2025-01-03',43),
(6,'Java','2025-01-03',41),
(7,'Java','2025-02-03',24),
(8,'C++','2025-02-03',23),
(9,'Python','2025-02-03',34),
(10,'Java','2025-02-04',42),
(11,'C++','2025-02-04',45),
(12,'Python','2025-02-04',59),
(13,'C++','2026-01-04',230),
(14,'Java','2026-01-04',764),
(15,'Python','2026-01-04',644),
(16,'C++','2026-01-06',240),
(17,'Java','2026-01-06',714),
(18,'Python','2026-01-06',624),
(19,'C++','2026-02-14',260),
(20,'Java','2026-02-14',721),
(21,'Python','2026-02-14',321),
(22,'C++','2026-02-24',134),
(23,'Java','2026-02-24',928),
(24,'Python','2026-02-24',525),
(25,'C++','2027-02-06',231);

输出:

Python|2025-02|93|2026-02|846
Java|2025-02|66|2026-02|1649
C++|2025-02|68|2026-02|394
Python|2025-01|66|2026-01|1268
Java|2025-01|53|2026-01|1478
C++|2025-01|107|2026-01|470

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Sqlite 解法, 执行用时: 11ms, 内存消耗: 3364KB, 提交时间: 2022-01-29

select job ,
('2025-'||mon) first_year_mon,sum(case when substr(date,1,4)='2025' then num else 0 end),
('2026-'||mon) second_year_mon,sum(case when substr(date,1,4)='2026' then num else 0 end)
from
(select id,job ,date,num,substr(date,6,2) as mon  from resume_info
)a
group by job ,mon
order by first_year_mon desc,job desc

Sqlite 解法, 执行用时: 11ms, 内存消耗: 3384KB, 提交时间: 2022-01-27

select
job
,('2025-'||mon) first_year_mon
,sum(case when substr(date,1,4)='2025' then num else 0 end) first_year_cnt
,('2026-'||mon) second_year_mon
,sum(case when substr(date,1,4)='2026' then num else 0 end) second_year_cnt
from
(
select
id,job,date,num,substr(date,6,2) mon
from resume_info
)a
group by job,mon
order by first_year_mon desc,job desc

Sqlite 解法, 执行用时: 11ms, 内存消耗: 3460KB, 提交时间: 2021-09-09

select
job
,('2025-'||mon) first_year_mon
,sum(case when substr(date,1,4)='2025' then num else 0 end) first_year_cnt
,('2026-'||mon) second_year_mon
,sum(case when substr(date,1,4)='2026' then num else 0 end) second_year_cnt
from
(
select
id,job,date,num,substr(date,6,2) mon
from resume_info
)a
group by job,mon
order by first_year_mon desc,job desc

Sqlite 解法, 执行用时: 11ms, 内存消耗: 3496KB, 提交时间: 2021-12-03

select a.job,first_year_mon,first_year_cnt,
second_year_mon,second_year_cnt
from (
select job,
substr(date,1,7) first_year_mon,
sum(num) first_year_cnt
from resume_info
where substr(date,1,4) = '2025'
group by job,substr(date,1,7)) a
left join (
select job,
substr(date,1,7) second_year_mon,
sum(num) second_year_cnt
from resume_info
where substr(date,1,4) = '2026'
group by job,substr(date,1,7)
)b
on a.job = b.job 
where substr(a.first_year_mon,6,7) = substr(b.second_year_mon,6,7)
order by first_year_mon desc,a.job desc

Sqlite 解法, 执行用时: 11ms, 内存消耗: 3620KB, 提交时间: 2022-01-05

with r1 as
(select job,date,substr(date,1,7) as first_year_mon,sum(num) as first_year_cnt
from resume_info
where date like '%2025%'
group by job,substr(date,1,7)),
r2 as (
select job,date,substr(date,1,7) as second_year_mon,sum(num) as second_year_cnt
from resume_info
where date like '%2026%'
group by job,substr(date,1,7)
)

select r1.job, first_year_mon,first_year_cnt,second_year_mon,second_year_cnt
from r1
left join r2
on r1.job=r2.job and substr(r1.date,6,2)=substr(r2.date,6,2)
order by first_year_mon DESC,r1.job DESC