SQL280. 实习广场投递简历分析(三)
描述
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 |
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
输入:
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